是什么型Htt presponseMessage和Htt的presponseException之间的差异 [英] What is the difference between HttpResponseMessage and HttpResponseException

查看:118
本文介绍了是什么型Htt presponseMessage和Htt的presponseException之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解都写样本code:

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

从小提琴,我真的没有看到他们之间的任何差别,那么什么是使用的目的的Htt presponseException

推荐答案

两者之间的主要区别是这样的。唯一的例外是有用的,立即停止加工和出口。例如,假设我有以下的code

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

如果这个code运行和我通过一个id不是present,它会立即停止加工,并返回404状态code。

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.

如果不是我回到了Htt presponseMessage,请求将很乐意继续处理的其余部分,并返回一个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.

由于达雷尔表示,例外的是在某些情况下,我想处理继续进行(如当客户发现)的情况下,而在其他我不知道。

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.

在这里,你可能想使用类似的Htt presponseMessage是在HTTP POST返回201状态code和设置位置标头的地方。在这种情况下,我想处理继续进行。这将会做这个code *

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

*注:如果您使用的是测试版位,你将创建一个新的Htt presponseMessage。我使用但是后来位需要您使用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.

在上面,我创建这台状态code至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.

希望这有助于

这篇关于是什么型Htt presponseMessage和Htt的presponseException之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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