Web API使用HttpResponseMessage与HttpResponseException的差异
来源:互联网 发布:夏河淘宝店 编辑:程序博客网 时间:2024/06/11 06:03
在 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.
}
- Web API使用HttpResponseMessage与HttpResponseException的差异
- Asp.net web Api源码分析-HttpResponseMessage
- ASP.NET Web API 配置返回的json字段的格式以及Action返回HttpResponseMessage类型
- .net Web API 2,return HttpResponseMessage with ObjectContent Json Type
- 聊聊WCF与Web Service的差异
- Web API的使用
- React实战-ReactJs与传统Web页面中Css的使用差异
- ThinkPHP5 与 ThinkPHP3.* 之间的使用差异
- 使用android-async-http下载图片时出现org.apache.http.client.HttpResponseException: Content-Type not allowed的错误
- J2EE Web架构与CS架构命名上的差异
- WEB APP、HYBRID APP与NATIVE APP的设计差异
- WEB APP、HYBRID APP与NATIVE APP的差异
- 移动Web App、Hybrid App与Native App的差异
- Java web开发中,转发与重定向的差异!
- WEB APP HYBRID APP 与 NATIVE APP的设计差异
- WEB APP、HYBRID APP与NATIVE APP的设计差异
- Web App、Hybrid App与Native App的设计差异
- AsyncTask的版本差异及其与Thread的使用场景
- The implement of Binary Search Tree (JAVA)
- oracle中的层次化查询
- Android中ListView与RadioButton结合----自定义单选列表
- Unity3D无缝场景切换解决方案 - 简单场景切换
- leetcode_6_ZigZag Conversion
- Web API使用HttpResponseMessage与HttpResponseException的差异
- 黑马程序员——应用管理笔记
- leetcode_7_Reverse Integer
- oracle层次化查询,你可能不知道的地方
- cocos2d-x 3.2 之 三消类游戏——万圣大作战 (第五篇)
- leetcode_8_String to Integer (atoi)
- 无法从用法中推导出方法“System.Data.Linq.Table 的解决
- A simple implement of the Tetris in JAVA
- leetcode_9_Palindrome Number