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 に答える