0

嗨,我正在 asp.net 中做我的项目现在我有一个按设计的错误页面现在如何在页面加载中发生错误时自动重定向到错误页面并找到该页面的错误代码

错误就像 404 403 400 500 错误代码

4

2 回答 2

0

You can use Global.asax and use the Application_Error method in there handle the exception and redirect. Here is what I am doing :

   protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Context.Error;

        if (ex is HttpUnhandledException)
        {
            ex = Context.Error.InnerException;
        }

        if (ex.GetType().Name == "FileNotFoundException")
        {
            //ignore
        }
        else
        {
            try
            {
                if ((ex as HttpException).GetHttpCode() == 404)
                {
                    // Page Not found, redirect
                    if (!ex.Message.Contains("error.aspx"))
                    {
                        Response.Status = "301 Moved Permanently";
                        Response.AddHeader("Location", "~/error.aspx");
                        Response.End();
                    }

                }
            }
            catch (Exception ThrownException)
            {
              //log
            }
        }
    }
于 2013-06-07T09:59:39.247 回答
0

Add global.asax page with the following code

protected void Application_Error(Object sender, EventArgs e)
{
    HttpException ex = (HttpException)Server.GetLastError();
    int httpCode = ex.GetHttpCode();
    if (httpCode == 404)
    {
        //do 404 code work here
    }
    if (httpCode == ???)
    {
        //do ??? code work here
    }
}
于 2013-06-07T09:59:46.057 回答