天天看點

Asp.Net Api2 過濾器的使用

1.注意:

   apiController控制器 對應的過濾器System.Web.Http.Filters.ActionFilterAttribute的過濾器

   MVC的Controller控制器 對應的過濾器System.Web.Http.Mvc.ActionFilterAttribute的過濾器

2.執行個體1

繼承類ActionFilterAttribute

public class FilterOneAttribute : ActionFilterAttribute
{
    //執行前
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        base.OnActionExecuting(actionContext);

        //傳回403拒絕
        HttpResponseMessage msg = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, "伺服器拒絕了你的請求");
        //設定相應對象,則不再執行Action
        actionContext.Response = msg;

        //傳回500伺服器錯誤
        HttpResponseMessage msg = actionContext.Request.CreateResponse(HttpStatusCode.InternalServerError, "您還沒有登入");
        actionContext.Response = msg;

        //讀取請求參數中的cookie
        HttpContextBase context = (HttpContextBase)actionContext.Request.Properties["MS_HttpContext"];
        string cookie = string.Join(",", context.Request.Cookies.AllKeys);
        HttpResponseMessage msg = actionContext.Request.CreateResponse(HttpStatusCode.OK, cookie);
        actionContext.Response = msg;
    }
}      

執行個體2(轉):

public class ActionFilter : ActionFilterAttribute
  {
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
      base.OnActionExecuting(actionContext);
      //擷取請求消息提資料
      Stream stream = actionContext.Request.Content.ReadAsStreamAsync().Result;
      Encoding encoding = Encoding.UTF8;
      stream.Position = 0;
      string responseData = "";
      using (StreamReader reader = new StreamReader(stream, encoding))
      {
        responseData = reader.ReadToEnd().ToString();
      }
      //反序列化進行處理
      var serialize = new JavaScriptSerializer();
      var obj = serialize.Deserialize<RequestDTO>(responseData);
      //在action執行前終止請求時,應該使用填充方法Response,将不傳回action方法體。
      if (obj == null)
        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, obj);

      if (string.IsNullOrEmpty(obj.PhoneType) || string.IsNullOrEmpty(obj.PhoneVersion)
        || string.IsNullOrEmpty(obj.PhoneID) || obj.StartCity < 1)
      {
        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, obj);
      }
    }
  }      
//
    // 摘要:
    //     表示所有操作篩選器特性的基類。
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IFilter
    {
        //
        // 摘要:
        //     初始化 System.Web.Http.Filters.ActionFilterAttribute 類的新執行個體。
        protected ActionFilterAttribute();

        //
        // 摘要:
        //     在調用操作方法之後發生。
        //
        // 參數:
        //   actionExecutedContext:
        //     操作執行的上下文。
        public virtual void OnActionExecuted(HttpActionExecutedContext actionExecutedContext);
        public virtual Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken);
        //
        // 摘要:
        //     在調用操作方法之前發生。
        //
        // 參數:
        //   actionContext:
        //     操作上下文。
        public virtual void OnActionExecuting(HttpActionContext actionContext);
        public virtual Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken);
    }      

繼續閱讀