2

我的 web.config 上有这个:

<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
    <error statusCode="403" redirect="NoAccess.htm" />
    <error statusCode="404" redirect="FileNotFound.htm" />
<customErrors/>

我将如何告诉 asp.net 我的 statusCode=404 并将我重定向到 NoAccess.htm?在 Global.asax Application_Error 我已经尝试过这一行:

Response.StatusCode = 400

但它仍然将我重定向到默认值 GenericErrorPage.htm。

有没有办法显式设置状态代码,以便 ASP.NET 将我重定向到我想要的自定义错误页面?

4

1 回答 1

4

如果您想从 global.asax 执行此操作,请尝试以下操作

protected void Application_Error(object sender, EventArgs e)
{
   HttpContext ctx = HttpContext.Current;
   Exception ex = ctx.Server.GetLastError();

   if (ex is HttpRequestValidationException)
   {
       ctx.Server.ClearError();
       ctx.Response.Redirect("/validationError.htm");
   }
   else
   {
        ctx.Server.ClearError();
        ctx.Response.Redirect("/NoAccess.htm"); 
   }      
}
于 2011-06-29T09:37:18.987 回答