MVC webapi,Action的分别拦截器

继承Controller,ApiController的拦截继承的接口不一样

webapi的拦截器:(继承System.Web.Http)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Configuration;

namespace Rongzi.Mi.Mvc
{
 
    public class IosApproveFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
    {
        private readonly bool IosApprove = Convert.ToBoolean(ConfigurationManager.AppSettings["IOSApprove"] == null ? "0" : ConfigurationManager.AppSettings["IOSApprove"]);
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            base.OnActionExecuting(actionContext);
        }


        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            if (IosApprove)
            {
                actionExecutedContext.ActionContext.Response.Headers.Add("IOSApprove", "true");
            }
            base.OnActionExecuted(actionExecutedContext);
        }
    }
}

Controller的拦截:(继承System.Web.Mvc)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Controllers;
using System.Configuration;
using System.Web.Mvc;

namespace Rongzi.Mi.Mvc
{

    public class IosApproveFilterAttribute : System.Web.Mvc.ActionFilterAttribute
    {
        private readonly bool IosApprove = ConfigurationManager.AppSettings["IOSApprove"] == "1" ? true : false;
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            base.OnActionExecuting(actionContext);
            
        }
        public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
        {
            if (IosApprove)
            {
                actionExecutedContext.HttpContext.Response.Headers.Add("IOSApprove", "true");
            }
            base.OnActionExecuted(actionExecutedContext);
        }
    }
}

可以发现他们的继承抽象类都不一样,虽然名称一样,但是是不同的命名空间。

 

posted @ 2017-07-27 09:38  hongdada  阅读(2468)  评论(0编辑  收藏  举报