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);
      }
    }
  }

3.抽象类ActionFilterAttribute定义

    //
    // 摘要:
    //     表示所有操作筛选器特性的基类。
    [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);
    }
原文地址:https://www.cnblogs.com/tianma3798/p/5318703.html