我有一个结合了MVC5和WebApi2的ASP.NET web项目。该项目包含一个web.config文件,该文件在system.webserver部分下定义了一些自定义的Http头。这些头对于MVC请求工作得很好,但是对于Api请求有一些重复值。
<customHeaders>
<clear />
<add name="Expires" value="-1" />
... other headers
</customHeaders>
所有的API请求都有重复的Http报头,如Cache-Control、Expires和Pragma,如下所示。
Cache-Control: no cache, no cache
Expires: -1,-1
似乎Web API框架默认设置了这些值。是否可以禁用默认的Api标头并使用配置设置,或者完全忽略Api请求的配置设置?对好的解决方案有什么想法吗?
像上面那样清除customHeaders元素,或者在添加doens之前先删除它们来覆盖它们也是不起作用的。
发布于 2018-01-09 19:14:32
您可以解决此问题。从web.config中删除customHeaders标记。在MVC5项目中,添加自定义操作筛选器属性。
public class AddHeadersFilterAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.AddHeader("header", "headerValue");
base.OnActionExecuting(filterContext);
}
}
用这个属性装饰你的MVC5控制器的BaseController。这应该会解决您的问题,并让Web API 2表现得像它应该的那样。
https://stackoverflow.com/questions/48154278
复制相似问题