首页 技术 正文
技术 2022年11月21日
0 收藏 520 点赞 3,236 浏览 4500 个字

HttpResponseException—–HTTP响应异常

如果Web API控制器抛出一个未捕捉的异常,会发生什么呢?在默认情况下,大多数异常都会转换为一个带有状态码500的内部服务器错误的HTTP响应。

这个HTTPResponseException类型是一个特殊的类型。这种异常会返回你在异常构造器中指定的任何HTTP状态码。例如,在以下方法中,如果这个id参数无效,那么会返回“404—未找到”

  public Product GetProduct(int id)
{
var item = repository.Get(id);
if (item == null)
//未找到返回一个404的状态码
throw new HttpResponseException(HttpStatusCode.NotFound);
return item;
}

为了响应进行更多的控制,你也可以构造整个响应消息,并用HTTPResponseMessage来包含它:

 public Product GetProduct(int id)
{
var item = repository.Get(id);
if (item == null)
//未找到返回一个404的状态码
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(String.Format("No product with ID={0}", id)),
ReasonPhrase = "Product ID Not Found"
};
throw new HttpResponseException(resp);
}
return item;
}

Exception Filters—异常过滤器

通过编写一个异常过滤器,你可以定制Web API如何处理异常。当一个控制器抛出一个未处理异常,且这个异常不是一个HttpResponseException异常时,一个异常过滤器会被执行。HttpResponseException类型是一个特殊情况,因为它是专门设计用来返回一个HTTP响应的。

异常过滤器实现System.Web.Http.Filters.IExceptionFilter接口。编写异常过滤器最简单的方法是通过System.Web.Fitlers.ExceptionFilterAttribute类进行派生,并重写OnException方法。

注意:ASP.NET Web API中的异常过滤器与ASP.NET MVC中是及其相似的。然后,它们被声明在不用的命名空间下,且功能也是独立的。特别强调以下,ASP.NET MVC中使用的HandlerErrorFilterAttribute不会处理Web API控制器抛出的异常。

以下是将NotImplementedException异常转换成HTTP状态码“501 – 未实现”的过滤器:

 namespace WebAPIDemo.Filter
{
public class NotImpleExceptionFilterAttribute:ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception is NotImplementedException)
actionExecutedContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.NotImplemented);
}
}
}

HttpActionExecutedContext对象的Response属性含有发送到客户端的HTTP响应消息

Registering  Exception Filters — 注册异常过滤器

以下是注册Web API异常过滤器的几种方式

1.通过Action注册

2.通过Controller注册

3.通过全局注册

要把过滤器应用特定的Action,在Action中添加过滤器的注解属性

 [NotImpleExceptionFilter]
public Product GetProduct(int id)
{
var item = repository.Get(id);
if (item == null)
//未找到返回一个404的状态码
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(String.Format("No product with ID={0}", id)),
ReasonPhrase = "Product ID Not Found"
};
throw new HttpResponseException(resp);
}
return item;
}

要把过滤器应用于一个控制器的所有Action,可以在Controller上添加过滤器的注解属性

[NotImpleExceptionFilter]
public class ProductController : ApiController
{
   
}

要全局性的把过滤器运用于所有的Web API控制器将该过滤器的一个实例添加GlobalConfiguration.Configuration.Filter集合。这个集合中的所有异常过滤器会应用于任何Web API控制器Action

public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
//注册全局异常过滤器
GlobalConfiguration.Configuration.Filters.Add(new NotImpleExceptionFilterAttribute());
}
}

HttpError—-HTTP错误

HttpError对象为在响应正文中返回错误消息提供了响应的方法。以下实例演示了如何用HttpError在响应中返回HTTP状态码“404–未找到”:

 public HttpResponseMessage GetProduct(int id)
{
var item = repository.Get(id);
if (item != null)
return Request.CreateResponse(HttpStatusCode.OK, item);
var message = string.Format("Product with id = {0} not found", id);
HttpError httpError = new HttpError(message);
return Request.CreateResponse(HttpStatusCode.NotFound, httpError);
}

在这个例子中,如果该方法成功,它会在HTTP响应中返回产品。但如果所请求的产品未找到,则HTTP响应会在请求体中包含一个HttpError。该响应看起来大致像这样

 HTTP/1.1  Not Found
Content-Type: application/json; charset=utf-
Date: Thu, Aug :: GMT
Content-Length: {
"Message": "Product with id = 12 not found"
}

注意。在这个例子中,HttpError会被序列化成JSON。使用HttpError的一个好处是,与其它强类型模型一样,会进行同样的“content-negotiation”(暂未实现)和序列过程

直接替代创建HttpError对象的一种方法是:你可以使用CreateErrorResponse方法:

 public HttpResponseMessage GetProduct(int id)
{
var item = repository.Get(id);
if (item != null)
return Request.CreateResponse(HttpStatusCode.OK, item);
var message = string.Format("Product with id = {0} not found", id);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
}

CreateErrorResponseSystem.Net.Http.HttpRequsetMessageExtensions类中被定义的一个扩展方法。本质上,CreateErrorResponse会创建一个HttpError实例,然后创建一个包含该HttpError的HttpResponseMessage

  Adding Custom Key-Values to HttpError 把自定义的键值添加到HttpError

  HttpError类实际上是个键–值集合,(派生与于Dictionary<String,Object>),因此你可以添加自己的键–值对

 public HttpResponseMessage GetProduct(int id)
{
var item = repository.Get(id);
if (item != null)
return Request.CreateResponse(HttpStatusCode.OK, item);
var message = string.Format("Product with id = {0} not found", id);
var err = new HttpError(message);
err["error_sub_code"] = ;
return Request.CreateErrorResponse(HttpStatusCode.NotFound, err);
}

Using HttpError with HttpResponseExceptionHttpResponseException的方式来使用HttpError

前面的例子是从Action返回一个HttpResponseMessage消息,但你也可以使用HttpResponseException来返回一个HttpError。这让你能够在正常成功情况下返回强类型模型,而在错误时,仍返回HttpError

 public Product GetProduct(int id)
{
var item = repository.Get(id);
if (item != null)
return item;
var message = string.Format("Product with id = {0} not found", id);
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893