WebAPI Action的几种返回值类型
| void | 返回204状态码 |
| HttpResponseMessage | Convert directly to an HTTP response message. |
| IHttpActionResult | Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message. |
| Other type | Write the serialized return value into the response body; return 200 (OK). |
1. void 返回204状态码
public void Get()
{ }
2.直接转化成http响应消息
public HttpResponseMessage Get()
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
response.Content = new StringContent("hello", Encoding.Unicode);
response.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = TimeSpan.FromMinutes()
};
//HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new { a=1,b=2});
//return response;
return response;
3.IHttpActionResult 调用 ExecuteAsync 创建HttpResponseMessage,最后实现 public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)方法
常用类https://msdn.microsoft.com/en-us/library/system.web.http.results(v=vs.118).aspx,也可以自定义实现IHttpActionResult接口。
public IHttpActionResult Get()
{
return NotFound();//Ok()
//return new TextResult("hello", Request); } }
public class TextResult : IHttpActionResult
{
string _value;
HttpRequestMessage _request; public TextResult(string value, HttpRequestMessage request)
{
_value = value;
_request = request;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage()
{
Content = new StringContent(_value),
RequestMessage = _request
}; return Task.FromResult(response);
}
}
4.使用其他类型
public Product Get()
{
return new Product { Id = , Name = "我的商品" };
} }
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
如果上述出现异常,无法返回404错误码,可以使用过滤器标签处理。
本文参考:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/action-results
WebAPI Action的几种返回值类型的更多相关文章
- C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解 ...
- WebApi 接口返回值类型详解 ( 转 )
使用过Webapi的园友应该都知道,Webapi的接口返回值主要有四种类型 void无返回值 IHttpActionResult HttpResponseMessage 自定义类型 此篇就围绕这四块分 ...
- WebApi接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.作为程序猿,我们都知道参数和返回值是编程领域不可分割的两大块,此前分享了 ...
- WebApi 接口返回值不困惑:返回值类型详解。IHttpActionResult、void、HttpResponseMessage、自定义类型
首先声明,我还没有这么强大的功底,只是感觉博主写的很好,就做了一个复制,请别因为这个鄙视我,博主网址:http://www.cnblogs.com/landeanfen/p/5501487.html ...
- (转)C# WebApi 接口返回值不困惑:返回值类型详解
原文地址:http://www.cnblogs.com/landeanfen/p/5501487.html 正文 前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi ...
- [转]C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
本文转自:http://www.cnblogs.com/landeanfen/p/5501487.html 阅读目录 一.void无返回值 二.IHttpActionResult 1.Json(T c ...
- C#进阶系列——WebApi接口返回值类型详解
阅读目录 一.void无返回值 二.IHttpActionResult 1.Json(T content) 2.Ok(). Ok(T content) 3.NotFound() 4.其他 5.自定义I ...
- Web Api 接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 WebApi 接口参数:传参详解,这篇博文内容本身很基础 ...
- ASP.NET Core中的Action的返回值类型
在Asp.net Core之前所有的Action返回值都是ActionResult,Json(),File()等方法返回的都是ActionResult的子类.并且Core把MVC跟WebApi合并之后 ...
随机推荐
- 【struts2】核心概念
1)FilterDispatcher Struts2的前端控制器,也是Struts2的MVC中的控制器部分.在实际开发中,只需要在web.xml中配置一次即可,如下所示: <filter> ...
- MVC摘记
2013年9月23日18:04:06 直接cshtml页面的js中使用 var customerId = @(ViewBag.Oppotunity as OppotunityDto==null ...
- shell脚本监控cpu/内存使用率 转
该脚本检测cpu和内存的使用情况,只需要调整memorySetting.cpuSetting.userEmail要发邮件报警的email地址即可 如果没有配置发邮件参数的哥们,已配置了的,直接飞到代码 ...
- Oracle 12C -- 不同容器之间切换
1.使用connect SQL> show con_name; CON_NAME ------------------------------ CDB$ROOT SQL> connect ...
- Microsoft.ACE.OLEDB.12.0 及其在 MSSQL中的使用
1.Microsoft.ACE.OLEDB.12.0 简介 就是一个数据访问接口,用于在office文件和非office应用程序间传输数据.例如 Microsoft Office Access 201 ...
- PhotoShop CS6实现照片背景虚化效果
在摄影实践中,虚化背景是突出主体的常用手段.但是由于消费级DC镜头的实际焦距都很短,因此实现浅景深而虚化背景的难度较大.如果我们希望用消费级DC也能达到虚化背景突出主体的效果,那么,Photoshop ...
- tomcat 8080 冲突 急速解决方法 -------屡试不爽
cmd 输入: netstat -ano|findstr 8080 记住 最后的端口号**** 再输入: taskkill /pid **** /f
- 【Unity】第11章 第三人称角色控制器和碰撞体
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 第三人称视角控制器涉及的相关概念有: 1.刚体(Rigidbody). 2.碰撞体(Collider).包括球体碰撞体( ...
- 【小白的CFD之旅】21 网格划分软件的选择
但是怎样才能获得流体计算网格呢?“工欲善其事必先利其器”,画网格该用什么器呢?小白决定找黄师姐请教一番. 小白找到黄师姐的时候,黄师姐正在电脑上忙着. “黄师姐,我发现网格划分软件有好多种,究竟哪种才 ...
- elastic search internal
Realtime Search with Lucene http://2010.berlinbuzzwords.de/sites/2010.berlinbuzzwords.de/files/busch ...