如何避免网站中的自定义/服务器错误?

时间:2012-03-02 07:30:43

标签: javascript jquery asp.net html url-rewriting

我有位于服务器上的asp.net Web应用程序我想避免来自我的网站的所有自定义和服务器错误。

为此,我使用了

<customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPage/TryAgainLater.aspx">   <error redirect="~/ErrorPage/PageNotFound.aspx" statusCode="404"/> </customErrors>

使用上面的代码可以避免一些问题。即

假设我的网站提供了“http://Exaple.com/Careers.aspx”页面

案例1. http://Exaple.com/Careersss.aspx“按照上述规则,它的工作正确”。

案例2. http://Exaple.com/!@##Careersss.aspx“不工作” 注意:这里我添加了特殊字符

案例3:http://Exaple.com/Careersss.aspxxxx“不工作” 注意:在“.aspx”

之后添加字符

案例4:http://Exaple.com/Careersss.aspx/!@!@!@! “这不是设计打破在这里”。 注意:添加带有特殊字符的“/”。

当用户获得案例2,3,4时请帮助我,然后他们会自动重定向到错误页面。

先谢谢。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

如果您不想更改IIS设置,可以在Global.asax中为404错误安装自己的处理程序。只需将此方法添加到我们的Global.asax.cs代码隐藏文件中:

protected void Application_EndRequest(object sender, EventArgs e)
{
    HttpResponse response = HttpContext.Current.Response;
    if (response.StatusCode == 404 && response.SubStatusCode == 0)
    {
        response.Redirect("/test/TryAgainLater.aspx?error=NotFound");
        response.End();
    }
}

如果它没有解决问题并且您仍然收到404错误页面,请将此设置添加到您的web.config文件中:

<configuration>
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>
相关问题