zoukankan      html  css  js  c++  java
  • Asp.Net Web API(四)

    HttpResponseException-----HTTP响应异常

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

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

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

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

     1 public Product GetProduct(int id)
     2 {
     3             var item = repository.Get(id);
     4             if (item == null)
     5             //未找到返回一个404的状态码
     6             {
     7                 var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
     8                 {
     9                     Content = new StringContent(String.Format("No product with ID={0}", id)),
    10                     ReasonPhrase = "Product ID Not Found"
    11                 };
    12                 throw new HttpResponseException(resp);
    13             }
    14             return item;
    15 }

    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 - 未实现”的过滤器:

     1 namespace WebAPIDemo.Filter
     2 {
     3     public class NotImpleExceptionFilterAttribute:ExceptionFilterAttribute
     4     {
     5         public override void OnException(HttpActionExecutedContext actionExecutedContext)
     6         {
     7             if (actionExecutedContext.Exception is NotImplementedException)
     8                 actionExecutedContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.NotImplemented);
     9         }
    10     }
    11 }

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

    Registering  Exception Filters --- 注册异常过滤器

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

           1.通过Action注册

           2.通过Controller注册

           3.通过全局注册

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

     1 [NotImpleExceptionFilter]
     2 public Product GetProduct(int id)
     3  {
     4           var item = repository.Get(id);
     5            if (item == null)
     6             //未找到返回一个404的状态码
     7             {
     8                 var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
     9                 {
    10                     Content = new StringContent(String.Format("No product with ID={0}", id)),
    11                     ReasonPhrase = "Product ID Not Found"
    12                 };
    13                 throw new HttpResponseException(resp);
    14             }
    15            return item;
    16 }

    要把过滤器应用于一个控制器的所有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--未找到”:

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

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

    1 HTTP/1.1 404 Not Found
    2 Content-Type: application/json; charset=utf-8
    3 Date: Thu, 09 Aug 2012 23:27:18 GMT
    4 Content-Length: 51
    5 
    6 {
    7   "Message": "Product with id = 12 not found"
    8 }

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

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

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

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

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

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

     1 public HttpResponseMessage GetProduct(int id)
     2 {
     3             var item = repository.Get(id);
     4             if (item != null)
     5                 return Request.CreateResponse(HttpStatusCode.OK, item);
     6             var message = string.Format("Product with id = {0} not found", id);
     7             var err = new HttpError(message);
     8             err["error_sub_code"] = 42;
     9             return Request.CreateErrorResponse(HttpStatusCode.NotFound, err);
    10 }

    Using HttpError with HttpResponseExceptionHttpResponseException的方式来使用HttpError

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

    1 public Product GetProduct(int id)
    2 {
    3             var item = repository.Get(id);
    4             if (item != null)
    5                 return item;
    6             var message = string.Format("Product with id = {0} not found", id);
    7             throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));
    8 }
  • 相关阅读:
    移动端上拉下拉刷新组件
    linux ftp搭建
    asp.net core 在Ubuntu 运行
    go can't find import: "github.com/** 错误
    WPF
    总结
    ASP.net
    计算器简单封装和ASP.net
    用户故事
    四则运算.结对编程
  • 原文地址:https://www.cnblogs.com/yan7/p/7774230.html
Copyright © 2011-2022 走看看