ASP.NET Web API 2:Action的返回类型
Web API控制器中的Action方法有如下几种返回类型:
- void
- HttpResponseMessage
- IHttpActionResult
- 其它类型
基于上面几种不同的返回类型,Web API创建HTTP响应消息的机制也不同。
| 返回类型 | Web API创建HTTP响应消息的机制 |
|---|---|
| void | 返回HTTP状态码204(无内容) |
| HttpResponseMessage | 直接转换成HTTP响应消息 |
| IHttpActionResult | 调用接口的ExecuteAsync方法创建一个HttpResponseMessage对象,然后转换成HTTP响应消息 |
| 其它类型 | 把序列化后的返回值写入响应正文,并且返回HTTP状态码200(OK) |
void
public class ReturnValueDemoController : ApiController
{
//返回类型为void
public void Get()
{
}
}
HTTP/1.1 No Content
Cache-Control: no-cache
Pragma: no-cache
Expires: -
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.
X-SourceFiles: =?UTF-?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtbw==?=
X-Powered-By: ASP.NET
Date: Wed, Mar :: GMT
HttpResponseMessage
public class ReturnValueDemoController : ApiController
{
//返回类型为HttpResponseMessage
public HttpResponseMessage Get()
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode .OK, "hello");
response.Content = new StringContent ( "hello wep api", Encoding .Unicode);
response.Headers.CacheControl = new CacheControlHeaderValue
{
MaxAge = TimeSpan .FromSeconds()
};
return response;
}
}
HTTP/1.1 OK
Cache-Control: max-age=
Content-Type: text/plain; charset=utf-
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.
X-SourceFiles: =?UTF-?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtbw==?=
X-Powered-By: ASP.NET
Date: Wed, Mar :: GMT
Content-Length: hello wep api
public HttpResponseMessage Get()
{
IQueryable <Product > products = repository.GetProducts();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode .OK, products);
return response;
}
HTTP/1.1 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-
Expires: -
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.
X-SourceFiles: =?UTF-?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtbw==?=
X-Powered-By: ASP.NET
Date: Wed, Mar :: GMT
Content-Length: [{"ProductId":,"Name":" 苹果","Description":null,"Price":1.0,"Category":"水果"},{"ProductId":,"Name":"鼠标","Description":null,"Price":50.0,"Category":"电脑配件"},{"ProductId":,"Name":"洗发水","Description":null,"Price":20.0,"Category":"日用品"}]
IHttpActionResult
public interface IHttpActionResult
{
Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken);
}
public class PlainTextResult : IHttpActionResult
{
private string text;
private HttpRequestMessage request; public PlainTextResult(string text, HttpRequestMessage request)
{
this .text = text;
this .request = request;
}
public Task < HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
HttpResponseMessage response=new HttpResponseMessage
{
Content = new StringContent (text, Encoding.Unicode),
RequestMessage = request
};
return Task .FromResult(response);
}
}
//返回类型是IHttpActionResult
public IHttpActionResult Get()
{
return new PlainTextResult( "plain text result" ,Request);
}
HTTP/1.1 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/plain; charset=utf-
Expires: -
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.
X-SourceFiles: =?UTF-?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtbw==?=
X-Powered-By: ASP.NET
Date: Wed, Mar :: GMT
Content-Length: plain text result
HTTP/1.1 Not Found
Cache-Control: no-cache
Pragma: no-cache
Expires: -
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.
X-SourceFiles: =?UTF-?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtb1w1?=
X-Powered-By: ASP.NET
Date: Wed, Mar :: GMT
Content-Length:
HTTP/1.1 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-
Expires: -
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.
X-SourceFiles: =?UTF-?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtb1wx?=
X-Powered-By: ASP.NET
Date: Wed, Mar :: GMT
Content-Length: {"ProductId":,"Name":" 苹果","Description":null,"Price":1.0,"Category":"水果"}
其它返回类型
public IQueryable < Product> GetpProducts()
{
return repository.GetProducts();
}
HTTP/1.1 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-
Expires: -
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.
X-SourceFiles: =?UTF-?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtb1w=?=
X-Powered-By: ASP.NET
Date: Wed, Mar :: GMT
Content-Length: [{"ProductId":,"Name":" 苹果","Description":null,"Price":1.0,"Category":"水果"},{"ProductId":,"Name":"鼠标","Description":null,"Price":50.0,"Category":"电脑配件"},{"ProductId":,"Name":"洗发水","Description":null,"Price":20.0,"Category":"日用品"}]
public Product GetProductById( int id)
{
Product product = repository.GetProductById(id);
if (product==null )
throw new HttpResponseException ( HttpStatusCode.NotFound); return product;
}
ASP.NET Web API 2:Action的返回类型的更多相关文章
- ASP.NET Web API 通过参数控制返回类型(JSON|XML)
一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml. 因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的 ...
- 如何让ASP.NET Web API的Action方法在希望的Culture下执行
在今天编辑推荐的<Hello Web API系列教程--Web API与国际化>一文中,作者通过自定义的HttpMessageHandler的方式根据请求的Accep-Language报头 ...
- ASP.NET Web API 如何通过程序控制返回xml还是json
雖然 ASP.NET Web API 內建支援 JSON 與 XML 兩種輸出格式,並依據瀏覽器端送出的 Accept 標頭自動決定回應的內容格式,不過有時候我們的確也需要讓程式來控制要回應哪種格式, ...
- 通过Web API调用Action时各种类型输入参数传递值的方法
本人微信公众号:微软动态CRM专家罗勇 ,回复280或者20180906可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . Dy ...
- ASP.NET Web API默认支持的媒体类型(SupportedMediaTypes)
JsonMediaTypeFormatter XmlMediaTypeFormatter ( application/xml text/xml) FormUrlEncodedMediaTypeFor ...
- ASP.NET Web API 数据提供系统相关类型及其关系
- 【ASP.NET Web API教程】1.1 第一个ASP.NET Web API
Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson ...
- HttpActionDescriptor,ASP.NET Web API又一个重要的描述对象
HttpActionDescriptor,ASP.NET Web API又一个重要的描述对象 通过前面对“HttpController的激活”的介绍我们已经知道了ASP.NET Web API通过Ht ...
- ASP.NET Web API 2中的错误处理
前几天在webapi项目中遇到一个问题:Controller构造函数中抛出异常时全局过滤器捕获不到,于是网搜一把写下这篇博客作为总结. HttpResponseException 通常在WebAPI的 ...
- ASP.NET Web API 控制器创建过程(二)
ASP.NET Web API 控制器创建过程(二) 前言 本来这篇随笔应该是在上周就该写出来发布的,由于身体跟不上节奏感冒发烧有心无力,这种天气感冒发烧生不如死,也真正的体会到了什么叫病来如山倒,病 ...
随机推荐
- sys模块python
sys模块 1: sys是python自带模块. 利用 import 语句输入sys 模块. 当执行import sys后, python在 sys.path 变量中所列目录中寻找 sys 模块文件. ...
- Struts2框架基础概念总结
一.struts2框架 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的 ...
- 在MySQL或者SQLServer中,添加对象后自动返回主键到对象模型中的配置方式
设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中.MySQL和SQLServer执行auto-generated key fiel ...
- Ubuntu14.04 Tab键自动补全
Unbuntu14.04 终端中使用Tab键不能自动补全 解决方案 1.利用vi编辑器打开 /etc/bash.bashrc文件(需要root权限) sudo vi /etc/bash.bashrc ...
- Java并发编程原理与实战二:并行&并发&多线程的理解
1.CPU的发展趋势: 核心数目依旧会越来越多,根据摩尔定律,由于单个核心性能提升有着严重的瓶颈问题,普通的PC桌面在2018年可能回到24核心. 2.并发和并行的区别: 所有的并发处理都有排队等候, ...
- iframe元素的学习(笔记)
什么是iframe:iframe元素即内联框架,iframe是内联的并且承前启后,对于外围的页面,iframe是一个普通的元素,对于iframe里面的内容,又是一个五脏俱全的页面.重下面的写法可以看出 ...
- 退役 AFO
noi滚粗了 D类没学校要 回去高考 此博客停止更新 此文章可能会继续更新 看心情 [upd 2017.11.13] 看完今年noip log级别数据结构终于出现辣! 看来noip以后又多了一大块考点 ...
- Let Me Count The Ways(Kickstart Round H 2018)
题目链接:https://code.google.com/codejam/contest/3324486/dashboard#s=p2 题目: 思路: 代码实现如下: #include <set ...
- 翻译:CommonJS的wiki
CommonJS的wiki资料原文 Modules/AsynchronousDefinition 异步定义的模块(国内一般叫AMD模块定义) STATUS: PROPOSAL 现状:提案 Implem ...
- sql_injection之基本get注入
1.代码篇 <?php error_reporting(0); include("../conn.php"); if(isset($_GET['id'])){ $id=$_G ...