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 的操作方式。
HttpResponseMessage
HttpResonseMessage 用来响应讯息并包含状态码及数据内容,如需要返回一个 HttpResonseMessage 的实例可以使用 Request 的扩充功能 CreateResponse 方法,如下
public HttpResponseMessage DeleteProductById(int id)
{
// do something...
return Request.CreateResponse(HttpStatusCode.OK);
}
当然也可以自行定义响应的状态码及数据内容,如下
public HttpResponseMessage DeleteProductById(int id)
{
// do something...
var response = Request.CreateResponse(HttpStatusCode.OK);
response.StatusCode = HttpStatusCode.OK;
response.Content = new StringContent("Delete Success!");
// 响应内容
return response;
}
另外 CreateResponse 扩充方法也提供了 CreateResponse<T> 泛型的回应方法 ,如下
public HttpResponseMessage GetProductById(int id)
{
IEnumerable<product> products = new ProductDao().GetProducts();
var product = products.Where(p => p.Id == id);
if (product.FirstOrDefault<product>() != null)
return Request.CreateResponse<Product>(HttpStatusCode.OK, product.First<Product>());
else
throw new HttpResponseException(HttpStatusCode.NotFound);
}
HttpResponseException
HttpResponseException 为处理例外之用,能够将指定的 HttpResponseMessage 返回至客户端,在客户端呼叫 Web API 发生错误时,客户端并不会得到一个空值或错误画面,故需要将错误包装成回复讯息而最基本的情况下可以只回复状态码,如下。
public HttpResponseMessage GetAllProducts()
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
当然也能够自己定义错误讯息内容,如下
public HttpResponseMessage PutProduct(int id, string name, string category, string price, int stock)
{
ProductDao productDao = new ProductDao(); if (productDao.UpdateProduct(id, name, category, price, stock))
return Request.CreateResponse(HttpStatusCode.OK);
else
{
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("Update Product Error"),
ReasonPhrase = "Server Error"
};
throw new HttpResponseException(response);
}
}
HttpResponseMessage和HttpResponseException (转)的更多相关文章
- Web API使用HttpResponseMessage与HttpResponseException的差异 HttpResponseMessage 返回类型
在 Web API 中提供了 HttpResponseMessage 与 HttpResponseException 用于处理返回讯息,HttpResponseMessage 用于返回一个来自于客户端 ...
- C#进阶系列——WebApi 异常处理解决方案
前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定不陌生,记得在介绍 AOP 的时候 ...
- WebApi异常
WebApi异常处理解决方案 前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定 ...
- (转)C# WebApi 异常处理解决方案
原文地址:http://www.cnblogs.com/landeanfen/p/5363846.html 一.使用异常筛选器捕获所有异常 我们知道,一般情况下,WebApi作为服务使用,每次客户端发 ...
- C#进阶系列——WebApi 异常处理解决方案(转)
出处:http://www.cnblogs.com/landeanfen/p/5363846.html 阅读目录 一.使用异常筛选器捕获所有异常 二.HttpResponseException自定义异 ...
- C#进阶系列——WebApi异常处理解决方案
阅读目录 一.使用异常筛选器捕获所有异常 二.HttpResponseException自定义异常信息 三.返回HttpError 四.总结 正文 为什么说是实践?因为在http://www.asp. ...
- ASP.NET Web API 异常处理 HttpResponseException 以及Angularjs获取异常信息并提示
一.HttpResponseException 如果一个Web API控制器抛出一个未捕捉异常,默认地,大多数异常都会被转化成一个带有状态码“500 – 内部服务器错误”的HTTP响应.HttpRes ...
- WebAPI 抛出HttpResponseException异常
[HttpGet] public List<UserInfo> GetList() { try { List<UserInfo> list = new List<User ...
- ASP.NET 5 WebApi 返回 HttpResponseMessage
首先,ASP.NET 5 没有了 MVC 和 WebApi 的区分,都属于 ASP.NET 5,从 Controller 的继承就可以看出,原来 ASP.NET WebApi 2 ValuesCont ...
随机推荐
- Amazon验证码机器算法识别
Amazon验证码识别 在破解Amazon的验证码的时候,利用机器学习得到验证码破解精度超过70%,主要是训练样本不够,如果在足够的样本下达到90%是非常有可能的. update后,样本数为2800多 ...
- 【转】libevent源码分析
libevent源码分析 转自:http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html 这两天没事,看了一下Memcached和l ...
- 01 Linux入门介绍
一.Linux 初步介绍 Linux的优点 免费的,开源的 支持多线程,多用户 安全性好 对内存和文件管理优越 系统稳定 消耗资源少 Linux的缺点 操作相对困难 一些专业软件以及游戏支持度不足 L ...
- 【MySQL】分页优化
前段时间由于项目的原因,对一个由于分页而造成性能较差的SQL进行优化,现在将优化过程中学习到关于分页优化的知识跟大家简单分享下. 分页不外乎limit,offset,在这两个关键字中,limit其实不 ...
- XML中& <> 单引号' 双引号 " 报错
由于xml中 这些字符是特殊字符,所以把&改成& 就行了 ,注意后面一定要带一个分号; < < 小于号> > ...
- android中MVP模式
http://blog.csdn.net/ysh06201418/article/details/46534799 Android App整体架构设计的思考 http://blog.csdn.ne ...
- [转]Installing SharePoint 2013 on Windows Server 2012 R2
转自:http://www.avivroth.com/2013/07/09/installing-sharepoint-2013-on-windows-server-2012-r2-preview/ ...
- xcode:关于Other Linker Flags
一.关于Other Linker Flags xcode中,在“Targets”选项下有Other Linker Flags选项,在这里可以填写xcode链接器的参数,如:-ObjC.-all_loa ...
- linux 网络编程比较好的文章
讲解epoll的, 算是比较详细的. 直接看使用部分就行. http://blog.csdn.net/xiajun07061225/article/details/9250579
- oracle 邮件发送
CREATE OR REPLACE PROCEDURE PRC_sendmail(p_receiver VARCHAR2, -- 邮件接收人 ...