[英]How to hide server version for custom errors in asp.net mvc?
Hi I am developing web application in .net mvc.
嗨,我正在开发.net mvc中的Web应用程序。 I am hiding server version using
<remove name="X-Powered-By" />
in customheaders. 我在customheaders中使用
<remove name="X-Powered-By" />
隐藏服务器版本。 It works fine and i am able to hide iis version. 它工作正常,我能够隐藏iis版本。 This works for pages that exists only.
这仅适用于存在的页面。 Also i have implemented
我也实现了
<customErrors defaultRedirect="Errorpage.html" mode="On">
<error statusCode="500" redirect="~/Login/Index" />
<error statusCode="400" redirect="~/Login/Index" />
</customErrors>
for custom errors. 自定义错误。 for example, if i try to access page that does not exists then i will be redirected to Errorpage.html but in this case server version is visible to users.
例如,如果我尝试访问不存在的页面,那么我将被重定向到Errorpage.html,但是在这种情况下,用户可以看到服务器版本。
Below is my global.asax code. 以下是我的global.asax代码。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
MvcHandler.DisableMvcResponseHeader = true;
}
protected void Application_PreSendRequestHeaders()
{
Response.Headers.Remove("Server");
Response.Headers.Set("Server", "");
Response.Headers.Remove("X-AspNet-Version"); //alternative to above solution
Response.Headers.Remove("X-AspNetMvc-Version"); //alternative to above solution
}
May i know how can i fix this issue in either cases pages that exists and pages that does not exists!
我可以知道在存在的页面和不存在的页面中如何解决此问题! may i get some help to fix this?
我可以得到一些帮助来解决这个问题吗? Any help would be greatly appreciated.
任何帮助将不胜感激。 Thank you.
谢谢。
You can create a custom error page for 404 and add this to your web.config file. 您可以为404创建一个自定义错误页面,并将其添加到您的web.config文件中。
<error statusCode="404" redirect="~/Home/PageNotFound" />
Where PageNotFound is your action in HomeController which returns view. 其中,PageNotFound是您在HomeController中返回视图的操作。
Now open your Global.asax.cs file to Application_Start, and add this code at the top: 现在,将您的Global.asax.cs文件打开到Application_Start,并在顶部添加以下代码:
MvcHandler.DisableMvcResponseHeader = true;
you can eliminate the "Server" header by adding a handler to PreSendRequestHeaders event like this: 您可以通过向PreSendRequestHeaders事件添加处理程序来消除“ Server”标头,如下所示:
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app != null &&
app.Context != null)
{
app.Context.Response.Headers.Remove("Server");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.