Web API使用HttpResponseMessage与HttpResponseException的差异

 在 Web API 中提供了 HttpResponseMessage 与 HttpResponseException 用于处理返回讯息,HttpResponseMessage 用于返回一个来自于客户端的请求结果讯息,你可以使用 HttpResponseMessage 自订返回的内容,HttpResponseException 则是以当发生例外时用来返回客户端错误讯息,例如一个 404 或 500 错误。

  其实用 HttpResponseMessage 也能够返回 404、500 等错误,那为何还需要使用 HttpResponseException 来返回错误? 参考此文章 提出了一个观点,文章中提到当呼叫 Web API 服务时发生了与预期上不同的错误时,理当应该中止程序返回错误讯息,这时对于错误的返回就该使用 HttpResponseException,而使用 HttpResponseMessage 则是代表着当客户端发送了一个工作请求而 Web API 正确的完成了这个工作,就能够使用 HttpResponseMessage 返回一个 201 的讯息,所以 HttpResponseMessage 与 HttpResponseException 在使用上根本的目标就是不同的,用 HttpResponseMessage 去返回一个例外错误也会让程序结构难以辨别且不够清晰,接着让我们看一下 HttpResponseMessage 与 HttpResponseException 的操作方式。 www.it165.net


HttpResponseMessage

  HttpResonseMessage 用来响应讯息并包含状态码及数据内容,如需要返回一个 HttpResonseMessage 的实例可以使用 Request 的扩充功能 CreateResponse 方法,如下

 

1. public HttpResponseMessage DeleteProductById(int id)
2. {
3. // do something...
4. return Request.CreateResponse(HttpStatusCode.OK);
5. }

  当然也可以自行定义响应的状态码及数据内容,如下

1. public HttpResponseMessage DeleteProductById(int id)
2. {
3. // do something...
4. var response = Request.CreateResponse(HttpStatusCode.OK);
5. response.StatusCode = HttpStatusCode.OK;
6. response.Content = new StringContent("Delete Success!");    // 响应内容
7. return response;
8. }

  如果需要响应列举对象可以使用 ObjectContent<T>,如下

1. public HttpResponseMessage GetAllProducts()
2. {
3. HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
4. response.Content = new ObjectContent<IEnumerable<product>>(
5. new ProductDao().GetProducts(),
6. new JsonMediaTypeFormatter());
7. return response;
8. }

  另外 CreateResponse 扩充方法也提供了 CreateResponse<T> 泛型的回应方法 ,如下

01. public HttpResponseMessage GetProductById(int id)
02. {
03. IEnumerable<product> products = new ProductDao().GetProducts();
04. var product = products.Where(p => p.Id == id);
05. if (product.FirstOrDefault<product>() != null)
06. return Request.CreateResponse<Product>(HttpStatusCode.OK, product.First<Product>());
07. else
08. throw new HttpResponseException(HttpStatusCode.NotFound);
09. }


HttpResponseException

  HttpResponseException 为处理例外之用,能够将指定的 HttpResponseMessage 返回至客户端,在客户端呼叫 Web API 发生错误时,客户端并不会得到一个空值或错误画面,故需要将错误包装成回复讯息而最基本的情况下可以只回复状态码,如下。

1. public HttpResponseMessage GetAllProducts()
2. {
3. throw new HttpResponseException(HttpStatusCode.NotFound);
4. }

  当然也能够自己定义错误讯息内容,如下

01. public HttpResponseMessage PutProduct(int id, string name, string category, string price, int stock)
02. {
03. ProductDao productDao = new ProductDao();
04.  
05. if (productDao.UpdateProduct(id, name, category, price, stock))
06. return Request.CreateResponse(HttpStatusCode.OK);
07. else
08. {
09. var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
10. {
11. Content = new StringContent("Update Product Error"),
12. ReasonPhrase = "Server Error"
13. };
14. throw new HttpResponseException(response);
15. }
16. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值