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 控制器创建过程(二) 前言 本来这篇随笔应该是在上周就该写出来发布的,由于身体跟不上节奏感冒发烧有心无力,这种天气感冒发烧生不如死,也真正的体会到了什么叫病来如山倒,病 ...
随机推荐
- 科学计算三维可视化---Traits(Event和button属性)
Event和button属性 是两个专门用于处理事件的change属性 Event属性和其他Trait属性不一样 Button属性是由Event属性继承而来的 Event监听 from traits. ...
- bzoj千题计划151:bzoj1131: [POI2008]Sta
http://www.lydsy.com/JudgeOnline/problem.php?id=1131 dp[i]=dp[fa[i]]-son[i]+n-son[i] #include<cst ...
- Redis学习九:Redis的发布订阅
发布订阅功能,redis也具备,但是要知道的是redis主要功能还是分布式的缓存功能,因此这种订阅发布功能很少用,有专门的kafka activemq 等消息中间件来完成,因此本文只是简单介绍,了解 ...
- ECMAScript6语法检查规范错误信息说明
项目中使用ECMAScript6的时候经查会使用语法检查,下面是常见错误信息的汇总: “Missing semicolon.” : “缺少分号.”, “Use the function form of ...
- Elasticsearch技术解析与实战(三)文档的聚合
1.计算每个tag下的商品数量 PUT /database/_mapping/product { "properties": { "tags": { " ...
- [转] malloc基本实现
任何一个用过或学过C的人对malloc都不会陌生.大家都知道malloc可以分配一段连续的内存空间,并且在不再使用时可以通过free释放掉.但是,许多程序员对malloc背后的事情并不熟悉,许多人甚至 ...
- [转]extern与头文件(*.h)的区别和联系
用#include可以包含其他头文件中变量.函数的声明,为什么还要extern关键字? 如果我想引用一个全局变量或函数a,我只要直接在源文件中包含#include<xxx.h> (xxx. ...
- JS中的作用域和闭包
作用域:在编程语言中,作用域控制着变量与参数的可见性及生命周期.JS确实有函数作用域,那意味着定义在函数中的参数和变量在函数外部是不可见的,而且在一个函数中的任何位置定义的变量在该函数中的任何地方都是 ...
- Uva 11549 - Calculator Conundrum 找规律加map
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Ubuntu自定义终端窗口位置
方法一: 自定义终端启动快捷键 具体方法是自定义一个快速启动终端的快捷键,附带设置终端启动时的位置参数.首先获得需要放置窗口的目标位置信息,可以通过终端命令“ xwininfo ”来获得.步骤是首先打 ...