闪电龟龟--笔记

万物寻其根,通其堵,便能解其困。
随笔 - 169, 文章 - 0, 评论 - 1, 阅读 - 79596
  博客园  :: 新随笔  :: 管理

WebAPI 返回值

Posted on   闪电龟龟  阅读(342)  评论(0编辑  收藏  举报

关于webapi的返回值有下面这几种:

1.void:我有返回值,这里不做解释,只要能够调用正确的方法便能够正确的执行。

2.IHttpActionResult:Json,Ok,NotFound,Contentm,Redirect等返回值,推荐使用这个。

3.HttpResponseMessage

4.自定义,这个也不讲解,相当于直接return。

 

关于IHttpActionResult返回数据

使用json返回数据

复制代码
public class LoginController : ApiController
    {
        public IHttpActionResult IHAR()
        {
            var l_EntityData = new { id = 1, name = "namejr", age = 12 };
            return Json(l_EntityData);  // json()
            //return Json<实体>(l_EntityData);  // json<T>()
        }
    }
复制代码

使用OK返回数据

复制代码
public class LoginController : ApiController
    {
        public IHttpActionResult IHAR()
        {
            var l_EntityData = new { id = 1, name = "namejr", age = 12 };
            return Ok(l_EntityData);  // json()
            //return Ok();  // json()表示只返回成功提示,不返回消息
            //return Ok<实体>(l_EntityData);  // json<T>()
        }
    }
复制代码

使用notfound()来返回找不到结果

复制代码
public IHttpActionResult IHAR()
        {
            var l_EntityData = new { id = 1, name = "namejr", age = 12 };
            if(l_EntityData==null)
            {
                return NotFound();  // 用来返回找不到记录
            }
            return Ok(l_EntityData);  // json()
            //return Ok();  // json()表示只返回成功提示,不返回消息
            //return Ok<实体>(l_EntityData);  // json<T>()
        }
复制代码

使用Reddirect来跳转页面

public class LoginController : ApiController
    {
        public IHttpActionResult IHAR()
        {
            return Redirect("http://www.baidu.com");  // 用来跳转页面
        }
    }

 使用HttpResponseMessage进行返回响应信息

复制代码
[HttpGet]
        public HttpResponseMessage InputFile()
        {
            try
            {
                string l_strFilePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Script/swagger.js");  // 获取路径
                FileStream l_streamFileStream = new FileStream(l_strFilePath, FileMode.Open);  // 打开文件
                // 构建HttpResponseMessage返回响应信息
                HttpResponseMessage l_response = new HttpResponseMessage(HttpStatusCode.OK);
                l_response.Content = new StreamContent(l_streamFileStream);//l_response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");  // 构建contenttype,如果这句话存在,会访问出错(仅使用postman进行测试过,其他的没测过,应该是构建类型错误)
                l_response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")  // 构建描述
                {
                    FileName = "Web Api Demo"
                };
                return l_response;  // 返回响应信息
            }
            catch
            {
                return new HttpResponseMessage(HttpStatusCode.NotFound);  // 返回找不到响应
            }
        }
复制代码

 

详细了解,请看:https://www.cnblogs.com/webapi/p/10540916.html

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2019-01-18 路由和HTTP方法
2019-01-18 alias 创建别名
2019-01-18 一个简单的flask程序
点击右上角即可分享
微信分享提示