HttpResponseMessage 和 HttpResponseException 有什么区别 [英] What is the difference between HttpResponseMessage and HttpResponseException

查看:20
本文介绍了HttpResponseMessage 和 HttpResponseException 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解两者并编写示例代码:

I tried to understand both and write sample code:

 public HttpResponseMessage Get()
 {
     var response = ControllerContext.Request
                         .CreateResponse(HttpStatusCode.BadRequest, "abc");

     throw new HttpResponseException(response);
 }

还有:

 public HttpResponseMessage Get()
 {
     return ControllerContext.Request
                        .CreateResponse(HttpStatusCode.BadRequest, "abc");
 }

从 Fiddle 来看,我真的没有看到它们之间有什么区别,那么使用 HttpResponseException 的目的是什么?

From Fiddle, I really didn't see any differences between them, so what is the purpose of using HttpResponseException?

推荐答案

两者的主要区别在于这一点.该异常对于立即停止处理并退出很有用.例如假设我有以下代码

The main difference between the two is this. The exception is useful to immediately stop processing and exit. For example assume I have the following code

public class CustomerController : ApiController {
  private ICustomerContext repo;

  public CustomerController(ICustomerContext repo) {
    this.repo = repo;
  }

  public Customer Get(int id) {
    var customer = repo.Customers.SingleOrDefault(c=>c.CustomerID == id);
    if (customer == null) {
      throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
    }
    return customer;
  }
}

如果此代码运行并且我传递了一个不存在的 id,它将立即停止处理并返回 404 状态代码.

If this code runs and I pass an id that is not present, it will immediately stop processing and return a status code of 404.

如果相反,我返回 HttpResponseMessage,请求将继续其处理的其余部分并返回 404.主要区别在于是否结束请求.

If instead I return HttpResponseMessage, the request will happily continue the rest of its processing and return a 404. The main difference being end the request or not.

正如 Darrel 所说,在某些情况下我希望继续处理(例如找到客户时)而在其他情况下我不想继续处理的情况下,异常很有用.

As Darrel said the exception is useful in cases where in some cases I want processing to continue (as in when customer is found) and in others I don't.

您可能想要使用 HttpResponseMessage 之类的地方是在 Http POST 中返回状态代码 201 并设置位置标头.在这种情况下,我确实希望继续处理.用这段代码就可以了.*

The place where you might want to use something like HttpResponseMessage is in an Http POST to return a status code of 201 and set the location header. In that case I do want processing to continue. That would would do with this code.*

public class CustomerController : ApiController {
  private ICustomerContext repo;

  public CustomerController(ICustomerContext repo) {
    this.repo = repo;
  }

  public HttpResponseMessage Post(Customer customer) {
    repo.Add(customer);
    repo.SaveChanges();
    var response = Request.CreateResponse(HttpStatusCode.Created, customer);
    response.Headers.Location = new Uri(Request.RequestUri, string.format("customer/{0}", customer.id));
    return response;
  }
}

*注意:如果您使用的是 beta 位,您将创建一个新的 HttpResponseMessage.但是,我正在使用后面的位,这要求您使用请求之外的 CreateResponse 扩展方法.

*note: If you are using the beta bits you would create a new HttpResponseMessage. I am using the later bits however which require you to use the CreateResponse extension method off of the Request.

上面,我正在创建一个响应,它将状态代码设置为 201,传入客户,然后设置位置标头.

Above, I am creating a response which sets the status code to 201, passes in the customer, and then sets the location header.

然后返回响应并继续处理请求.

The response is then returned and the request continues processing.

希望能帮到你

这篇关于HttpResponseMessage 和 HttpResponseException 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆