我在 web.config 中有以下内容,但是在服务器上发布到 IIS 7.5 后,在 IIS -> HTTP Response Headers
下找不到它们.
我发现的是 web.config
服务器上也没有这些条目,但它们在发布之前就在那里。所以我只能说发布过程把它们去掉了,但是web.config
里面什么都没有。转换文件以删除它们。那么为什么它们从已发布的“web.config”中消失了呢?
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
最佳答案
你确定 web.config 是最好的地方吗?我倾向于更喜欢自定义 ActionFilter。这使您有机会选择何时(在什么方法上)您希望逻辑发生,并且还提供更多控制(特别是异常处理,在 Action 生命周期的各个阶段做什么)。
Microsoft recommends using this approach for invocations that occur before Action execution.
一些示例代码
public class CustomFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//add in your custom headers
filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Methods", "GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS");
base.OnActionExecuting(filterContext);
}
public void OnException(ExceptionContext filterContext)
{
//do some cool exception handling here
}
}
关于asp.net-mvc-4 - 如何使用 web.config 设置自定义标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26023580/