标签:
Web API控制器中的Action方法有如下几种返回类型:
返回类型 | 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 204 No Content Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtbw==?= X-Powered-By: ASP.NET Date: Wed, 04 Mar 2015 14:34:09 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(600) }; return response; } }
HTTP/1.1 200 OK Cache-Control: max-age=600 Content-Type: text/plain; charset=utf-16 Vary: Accept-Encoding Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtbw==?= X-Powered-By: ASP.NET Date: Wed, 04 Mar 2015 15:14:05 GMT Content-Length: 26 hello wep api
public HttpResponseMessage Get() { IQueryable <Product > products = repository.GetProducts(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode .OK, products); return response; }
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtbw==?= X-Powered-By: ASP.NET Date: Wed, 04 Mar 2015 15:37:51 GMT Content-Length: 264 [{"ProductId":1,"Name":" 苹果","Description":null,"Price":1.0,"Category":"水果"},{"ProductId":2,"Name":"鼠标","Description":null,"Price":50.0,"Category":"电脑配件"},{"ProductId":3,"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 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: text/plain; charset=utf-16 Expires: -1 Vary: Accept-Encoding Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtbw==?= X-Powered-By: ASP.NET Date: Wed, 04 Mar 2015 16:17:30 GMT Content-Length: 34 plain text result
HTTP/1.1 404 Not Found Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtb1w1?= X-Powered-By: ASP.NET Date: Wed, 04 Mar 2015 16:54:21 GMT Content-Length: 0
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtb1wx?= X-Powered-By: ASP.NET Date: Wed, 04 Mar 2015 16:53:16 GMT Content-Length: 82 {"ProductId":1,"Name":" 苹果","Description":null,"Price":1.0,"Category":"水果"}
其它返回类型
public IQueryable < Product> GetpProducts() { return repository.GetProducts(); }
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcV29ya1NwYWNlXERvdE5ldFxNdmNEZW1vXE12Y0RlbW8uV2ViVUlcYXBpXFJldHVyblZhbHVlRGVtb1w=?= X-Powered-By: ASP.NET Date: Wed, 04 Mar 2015 17:03:13 GMT Content-Length: 264 [{"ProductId":1,"Name":" 苹果","Description":null,"Price":1.0,"Category":"水果"},{"ProductId":2,"Name":"鼠标","Description":null,"Price":50.0,"Category":"电脑配件"},{"ProductId":3,"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; }
标签:
原文地址:http://www.cnblogs.com/daretodream2014/p/4314738.html