1

アプリケーションが例外をスローしたときにエラー ページにリダイレクトしたいのですが、そのページにリダイレクトされません。これが私のコードです:

protected void Application_Error(object sender, EventArgs e)
    {

        StringBuilder theBody = new StringBuilder();

        Exception exception = Server.GetLastError();

        StackTrace st = new StackTrace(exception, true);
        // Get the top stack frame
        StackFrame frame = st.GetFrame(0);
        // Get the line number from the stack frame
        int line = frame.GetFileLineNumber();

        string file = frame.GetFileName();

        MethodBase site = exception.TargetSite;
        string methodName = site == null ? null : site.Name;

        string type = exception.GetType().ToString();

        theBody.Append("Line: " + line + "<br/>");
        theBody.Append("Method: " + methodName + "<br/>");
        theBody.Append("File: " + methodName + "<br/>");
        theBody.Append("IP: " + Request.ServerVariables["REMOTE_HOST"] + "<br/>");
        theBody.Append("Error Message: " + exception.Message + "<br/>");
        theBody.Append("Error Stack:" + exception.StackTrace);

            Response.Redirect("~/404.aspx", false);
            Context.ApplicationInstance.CompleteRequest();


       ...
    }

そして、テストページでこのように試します

 try
        {

            throw new Exception("Test exception!!");
        }
        catch (Exception)
        {


        }

404.aspx をリダイレクトせず、テスト ページにとどまります。404.aspx をリダイレクトするにはどうすればよいですか?

4

1 に答える 1

1

このリンクhttp://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-csで説明されているように web.config に設定します

<system.web>
        <customErrors mode="On"
                      defaultRedirect="~/ErrorPages/Oops.aspx" />

        ...
    </system.web>
于 2013-03-28T10:27:46.953 に答える