继承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); } }}
可以发现他们的继承抽象类都不一样,虽然名称一样,但是是不同的命名空间。