Web API控制器中的Action方法有以下幾種返回類型:json
返回類型 | Web API建立HTTP響應消息的機制 |
---|---|
void | 返回HTTP狀態碼204(無內容) |
HttpResponseMessage | 直接轉換成HTTP響應消息 |
IHttpActionResult | 調用接口的ExecuteAsync方法建立一個HttpResponseMessage對象,而後轉換成HTTP響應消息 |
其它類型 | 把序列化後的返回值寫入響應正文,而且返回HTTP狀態碼200(OK) |
voidapi
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
HttpResponseMessageapp
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":"水果"}
其它返回類型this
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; }