<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      ASP.NET Web API之消息[攔截]處理

      標(biāo)題相當(dāng)難取,內(nèi)容也許和您想的不一樣,而且網(wǎng)上已經(jīng)有很多這方面的資料了,我不過是在實踐過程中作下記錄。廢話少說,直接開始。

      Exception


      當(dāng)服務(wù)端拋出未處理異常時,most exceptions are translated into an HTTP response with status code 500, Internal Server Error.當(dāng)然我們也可以拋出一個特殊的異常HttpResponseException,它將被直接寫入響應(yīng)流,而不會被轉(zhuǎn)成500。

      public Product GetProduct(int id)
      {
          Product item = repository.Get(id);
          if (item == null)
          {
              throw new HttpResponseException(HttpStatusCode.NotFound);
          }
          return item;
      }

      有時要對服務(wù)端異常做一封裝,以便對客戶端隱藏具體細(xì)節(jié),或者統(tǒng)一格式,那么可創(chuàng)建一繼承自System.Web.Http.Filters.ExceptionFilterAttribute的特性,如下:

      public class APIExceptionFilterAttribute : ExceptionFilterAttribute
      {
          public override void OnException(HttpActionExecutedContext context)
          {
              //業(yè)務(wù)異常
              if (context.Exception is BusinessException)
              {
                  context.Response = new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.ExpectationFailed };
                  BusinessException exception = (BusinessException)context.Exception;
                  context.Response.Headers.Add("BusinessExceptionCode", exception.Code);
                  context.Response.Headers.Add("BusinessExceptionMessage", exception.Message);
              }
              //其它異常
              else
              {
                  context.Response = new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.InternalServerError };
              }
          }
      }

      然后將該Attribute應(yīng)用到action或controller,或者GlobalConfiguration.Configuration.Filters.Add(new APIExceptionFilterAttribute());使之應(yīng)用于所有action(If you use the "ASP.NET MVC 4 Web Application" project template to create your project, put your Web API configuration code inside the WebApiConfig class, which is located in the App_Start folder:config.Filters.Add(newProductStore.NotImplExceptionFilterAttribute());)。當(dāng)然,在上述代碼中,我們也可以在OnException方法中直接拋出HttpResponseException,效果是一樣的。

      Note: Something to have in mind is that the ExceptionFilterAttribute will be ignored if the ApiController action method throws a HttpResponseException;If something goes wrong in the ExceptionFilterAttribute and an exception is thrown that is not of type HttpResponseException, a formatted exception will be thrown with stack trace etc to the client.

      .net還內(nèi)置了HttpError這個類,若想返回格式化對象(如json、xml等),用起來更方便。The HttpError class is actually a key-value collection (it derives from Dictionary<string, object>), so you can add your own key-value pairs.

      以上知識主要來自Exception Handling in ASP.NET Web API

      ActionFilterAttribute、ApiControllerActionInvoker 


      有時要在action執(zhí)行前后做額外處理,那么ActionFilterAttribute和ApiControllerActionInvoker就派上用場了。比如客戶端請求發(fā)過來的參數(shù)為用戶令牌字符串token,我們要在action執(zhí)行之前先將其轉(zhuǎn)為action參數(shù)列表中對應(yīng)的用戶編號ID,如下: 

      public class TokenProjectorAttribute : ActionFilterAttribute
      {
          private string _userid = "userid";
          public string UserID
          {
              get { return _userid; }
              set { _userid = value; }
          }
      
          public override void OnActionExecuting(HttpActionContext actionContext)
          {
              if (!actionContext.ActionArguments.ContainsKey(UserID))
              {
                  //參數(shù)列表中不存在userid,寫入日志
                  //……
                  var response = new HttpResponseMessage();
                  response.Content = new StringContent("用戶信息轉(zhuǎn)換異常.");
                  response.StatusCode = HttpStatusCode.Conflict;
                  //在這里為了不繼續(xù)走流程,要throw出來,才會立馬返回到客戶端
                  throw new HttpResponseException(response);
              }
              //userid系統(tǒng)賦值
              actionContext.ActionArguments[UserID] = actionContext.Request.Properties["shumi_userid"];
              base.OnActionExecuting(actionContext);
          }
      
          public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
          {
              base.OnActionExecuted(actionExecutedContext);
          }
      }

      ActionFilterAttribute如何應(yīng)用到action,和前面的ExceptionFilterAttribute類似。

      ApiControllerActionInvoker以上述Exception為例:

      public class ServerAPIControllerActionInvoker : ApiControllerActionInvoker
      {
          public override Task<HttpResponseMessage> InvokeActionAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
          {
              //對actionContext做一些預(yù)處理
              //……
              var result = base.InvokeActionAsync(actionContext, cancellationToken);
              if (result.Exception != null && result.Exception.GetBaseException() != null)
              {
                  var baseException = result.Exception.GetBaseException();
      
                  if (baseException is BusinessException)
                  {
                      return Task.Run<HttpResponseMessage>(() =>
                      {
                          var response = new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
                          BusinessException exception = (BusinessException)baseException;
                          response.Headers.Add("BusinessExceptionCode", exception.Code);
                          response.Headers.Add("BusinessExceptionMessage", exception.Message);
                          return response;
                      });
                  }
                  else
                  {
                      return Task.Run<HttpResponseMessage>(() => new HttpResponseMessage(HttpStatusCode.InternalServerError));
                  }
              }
              return result;
          }
      }

      然后注冊至GlobalConfiguration.Configuration.Services中。由于ApiControllerActionInvoker乃是影響全局的,所以若要對部分action進(jìn)行包裝處理,應(yīng)該優(yōu)先選擇ActionFilterAttribute。另外ApiControllerActionInvoker在ActionFilterAttribute之前處理。

      DelegatingHandler


      前面的攔截都發(fā)生在請求已被路由至對應(yīng)的action后發(fā)生,有一些情況需要在路由之前就做預(yù)先處理,或是在響應(yīng)流返回過程中做后續(xù)處理,這時我們就要用到DelegatingHandler。比如對請求方的身份驗證,當(dāng)驗證未通過時直接返回錯誤信息,否則進(jìn)行后續(xù)調(diào)用。

      public class AuthorizeHandler : DelegatingHandler
      {
          private static IAuthorizer _authorizer = null;
      
          static AuthorizeHandler()
          { }
      
          protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
          {
              if (request.Method == HttpMethod.Post)
              {
                  var querystring = HttpUtility.ParseQueryString(request.RequestUri.Query);
                  var formdata = request.Content.ReadAsFormDataAsync().Result;
                  if (querystring.AllKeys.Intersect(formdata.AllKeys).Count() > 0)
                  {
                      return SendError("請求參數(shù)有重復(fù).", HttpStatusCode.BadRequest);
                  }
              }
              //請求方身份驗證
              AuthResult result = _authorizer.AuthRequest(request);
              if (!result.Flag)
              {
                  return SendError(result.Message, HttpStatusCode.Unauthorized);
              }
              request.Properties.Add("shumi_userid", result.UserID);
              return base.SendAsync(request, cancellationToken);
          }
      
          private Task<HttpResponseMessage> SendError(string error, HttpStatusCode code)
          {
              var response = new HttpResponseMessage();
              response.Content = new StringContent(error);
              response.StatusCode = code;
      
              return Task<HttpResponseMessage>.Factory.StartNew(() => response);
          }
      }

      這里的DelegatingHandler用于服務(wù)端,其實DelegatingHandler也可以在發(fā)起調(diào)用時使用,HttpClient可接收一個DelegatingHandler作為消息處理器。

      參考資料:

      轉(zhuǎn)載請注明原文出處:http://www.rzrgm.cn/newton/p/3238082.html

      posted @ 2013-08-05 15:46  萊布尼茨  閱讀(8282)  評論(5)    收藏  舉報
      主站蜘蛛池模板: 精品亚洲精品日韩精品| 久久精品国产91精品亚洲| 国产女人水真多18毛片18精品 | 国产精品久久自在自线不卡| 成全影院电视剧在线观看| 国内精品久久久久影视| 国内精品久久久久电影院| 亚洲精品天堂在线观看| 中文字幕在线国产精品| 开心激情站开心激情网六月婷婷| 亚洲国产五月综合网| 隆安县| 国产精品爽爽久久久久久竹菊| 日韩精品一区二区三区久| 日本边吃奶边摸边做在线视频| 国产免费一区二区不卡| 男人扒开添女人下部免费视频| 国产在线观看黄| 国产一卡2卡三卡4卡免费网站| 亚洲熟妇中文字幕五十路| 欧洲精品色在线观看| 狠狠色噜噜狠狠狠狠av不卡| 中文字幕乱码十国产乱码| 国产精品一区二区AV| 成人中文在线| 国产精品黄色一区二区三区| 人人做人人澡人人人爽| 国产成人无码区免费内射一片色欲| 九九热精彩视频在线免费| 加勒比无码人妻东京热 | 精品国产AV无码一区二区三区| 午夜免费国产体验区免费的| 国产精品一区二区 尿失禁| 亚洲日韩国产一区二区三区在线| 香港日本三级亚洲三级| 久久久无码一区二区三区| 亚洲精品一区二区三区大| 中文字幕永久精品国产| 灵台县| 久久99精品久久久大学生| 最新国产精品拍自在线观看|