【Web API系列教程】1.2 — Web API 2中的Action Results

前言

本节的主题是ASP.NET Web API如何将控制器动作的返回值转换成HTTP的响应消息。

Web API控制器动作可以返回下列的任何值:
1, void
2, HttpResponseMessage
3, IHttpActionResult
4, Some other type

取决于返回的以上哪一种,Web API使用不同的机制来创建HTTP响应。

Return typeHow Web API creates the response
voidReturn empty 204 (No Content)
HttpResponseMessageConvert directly to an HTTP response message.
IHttpActionResultCall ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message.
Other typeWrite the serialized return value into the response body; return 200 (OK).

本节的剩余部分将详细描述每种返回值。

void

如果返回类型是type,Web API就会用状态码204(No Content)返回一个空HTTP响应。

示例控制器:

public class ValuesController : ApiController
{
    public void Post()
    {
    }
}

HTTP相应:

HTTP/1.1 204 No Content
Server: Microsoft-IIS/8.0
Date: Mon, 27 Jan 2014 02:13:26 GMT

HttpResponseMessage

如果一个动作返回HttpResponseMessage,Web API就通过HttpResponseMessage的属性构造成消息从而直接将返回值转换成HTTP响应。

public class ValuesController : ApiController
{
    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(20)
        };
        return response;
    } 
}

相应:

HTTP/1.1 200 OK
Cache-Control: max-age=1200
Content-Length: 10
Content-Type: text/plain; charset=utf-16
Server: Microsoft-IIS/8.0
Date: Mon, 27 Jan 2014 08:53:35 GMT

hello

如果传递一个域模型给CreateResponse方法,Web API会使用媒体格式(media formatter)将序列化模型写入到响应体中。

public HttpResponseMessage Get()
{
    // Get a list of products from a database.
    IEnumerable<Product> products = GetProductsFromDB();

    // Write the list to the response body.
    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, products);
    return response;
}

IHttpActionResult

IHttpActionResult接口在Web API 2中被引进。本质上,它定义了一个HttpResponseMessage工厂。以下是使用IHttpActionResult接口的好处:
1, 简单你的控制器的单元测试
2, 为创建HTTP相应将公共逻辑移动到单独的类
3, 通过隐藏构建相应的底层细节,使控制器动作更清晰

IHttpActionResult包含一个单独的方法ExecuteAsync,它会异步地创建一个HttpResponseMessage实例:

public interface IHttpActionResult
{
    Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken);
} 

如果一个控制器动作返回IHttpActionResult,Web API会调用ExecuteAsync方法来创建HttpResponseMessage。然后将HttpResponseMessage转换到HTTP相应消息里。

以下是一个IHttpActionResult的简单执行,它创建一个文本相应:

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);
    }
}

控制器动作示例:

public class ValuesController : ApiController
{
    public IHttpActionResult Get()
    {
        return new TextResult("hello", Request);
    }
}

相应:

HTTP/1.1 200 OK
Content-Length: 5
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/8.0
Date: Mon, 27 Jan 2014 08:53:35 GMT

hello

更通常的情况是,你会使用System.Web.Http.Results命名空间下定义的IHttpActionResult实现。

在接下来的示例中,如果请求没有匹配到任何已存在的产品ID,控制器就会调用ApiController.NotFound来创建一个404(Not Found)响应。否则,控制器会调用ApiController.OK,它会调用一个意为包含该产品的200(OK)相应。

public IHttpActionResult Get (int id)
{
    Product product = _repository.Get (id);
    if (product == null)
    {
        return NotFound(); // Returns a NotFoundResult
    }
    return Ok(product);  // Returns an OkNegotiatedContentResult
}

Other Return Types

对于其他所有返回类型,Web API使用媒体格式(media formatter)来序列化返回值。Web API将序列化值写入到响应体中。响应状态码是200(OK)。

public class ProductsController : ApiController
{
    public IEnumerable<Product> Get()
    {
        return GetAllProductsFromDB();
    }
}

该实现的缺点在于你不能直接返回一个错误码,比如404。

Web API通过在请求中使用Accept头来选择格式。

示例请求:

GET http://localhost/api/products HTTP/1.1
User-Agent: Fiddler
Host: localhost:24127
Accept: application/json

示例相应:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
Date: Mon, 27 Jan 2014 08:53:35 GMT
Content-Length: 56

[{"Id":1,"Name":"Yo-yo","Category":"Toys","Price":6.95}]
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
.NET Web API 是一种用于构建 RESTful Web 服务的框架。在 Web API ,我们可以使用标准的 HTTP 方法(GET、POST、PUT、DELETE 等)来操作资源,通过 URL 参数和请求体传递数据。接下来,我将向您介绍如何在 .NET Web API 接收参数。 首先,我们需要创建一个 Web API 控制器。在控制器,我们可以定义多个 Action 方法,每个方法对应一个 HTTP 请求。 例如,我们创建一个名为 "ValuesController" 的控制器,并定义一个名为 "Get" 的 Action 方法,用于处理 HTTP GET 请求: ```csharp public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } } ``` 在上面的代码,我们返回了一个包含两个字符串的数组作为响应结果。 现在,我们来看一下如何接收参数。假设我们想要实现一个查询指定 ID 的资源的功能,我们可以在控制器添加一个名为 "GetById" 的 Action 方法,并使用 id 参数来指定资源的 ID: ```csharp public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string GetById(int id) { return "value" + id; } } ``` 在上面的代码,我们使用 "int id" 参数来接收请求 URL 的 ID 参数,并返回一个包含 "value" 和 ID 的字符串作为响应结果。 如果我们想要接收多个参数,可以在方法添加多个参数: ```csharp public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string GetById(int id, string name) { return "Hello, " + name + "! Your ID is " + id; } } ``` 在上面的代码,我们添加了一个名为 "name" 的字符串类型参数,用于接收请求 URL 的 name 参数。 除了从 URL 接收参数外,我们还可以从请求体接收参数。在 HTTP POST 请求,我们可以通过请求体来传递数据。在 Web API ,我们可以使用 [FromBody] 属性来指定请求体的参数。 例如,我们可以定义一个名为 "Create" 的 Action 方法,用于处理 HTTP POST 请求: ```csharp public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string GetById(int id) { return "value" + id; } // POST api/values public void Create([FromBody] string value) { // Do something with the value } } ``` 在上面的代码,我们添加了一个名为 "value" 的字符串类型参数,并使用 [FromBody] 属性来指定它是从请求体接收的参数。 最后,我们需要注意的是,如果我们需要接收复杂的对象类型参数,可以使用 [FromBody] 属性来指定对象的类型,并在方法使用该类型的实例来接收参数。例如: ```csharp public class User { public int Id { get; set; } public string Name { get; set; } } public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string GetById(int id) { return "value" + id; } // POST api/values public void Create([FromBody] User user) { // Do something with the user object } } ``` 在上面的代码,我们定义了一个名为 "User" 的类,并在 "Create" 方法使用它来接收从请求体传递的参数。通过这种方式,我们可以轻松地实现 Web API 的参数接收功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值