Xax*_*xum 40 c# asp.net-mvc http-headers x-frame-options asp.net-mvc-4
我正在尝试将X-Frame-Options标头(值设置为"DENY")添加到我的MVC 4应用程序中.我环顾四周,似乎这是为所有页面添加最干净的方式.
但是,当我添加此代码时,它将无法构建.有错误OnResultExecuting
的
"找不到合适的方法来覆盖."
public class XframeOptions : ActionFilterAttribute
{
public override void OnResultExecuting(
System.Web.Mvc.ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.AddHeader(
"X-Frame-Options", "DENY");
}
}
Run Code Online (Sandbox Code Playgroud)
如果这是最干净的方法,我该如何解决此错误?有没有更好的方法来处理MVC 4应用程序?
rob*_*ich 126
如果您需要每个页面都不需要自定义HttpModule或ActionFilter. https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options详细介绍了一个更简单的解决方案:
要配置IIS以发送X-Frame-Options标头,请将此站点的Web.config文件添加到此文件中:
<system.webServer>
...
<httpProtocol>
<customHeaders>
<add name="X-Frame-Options" value="SAMEORIGIN" />
</customHeaders>
</httpProtocol>
...
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 14
确保你继承自correct class
:
public class XframeOptions : System.Web.Mvc.ActionFilterAttribute
Run Code Online (Sandbox Code Playgroud)
在ASP.NET MVC 4中,Web API具有不同的命名空间,因为您没有明确指定命名空间,我猜编译器选择了错误的类:
System.Web.Http.Filters.ActionFilterAttribute
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以做到这一点.创建一个自定义的HttpModule,如下所示:
public class XframeOptionsModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += this.OnPreSendRequestHeaders;
}
private void OnPreSendRequestHeaders(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("x-frame-options", "Deny");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在web.config中注册此模块
<modules >
<add name ="XframeOptions" type="your module's full type info"/>
</modules>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
36491 次 |
最近记录: |