【问题标题】:redirect to error page when error occurs and access denied in page发生错误时重定向到错误页面并在页面中拒绝访问
【发布时间】:2013-06-07 09:46:45
【问题描述】:

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

错误类似于 404 403 400 500 错误代码

【问题讨论】:

    标签: c# asp.net .net runtime-error


    【解决方案1】:

    您可以使用Global.asax 并使用其中的 Application_Error 方法处理异常和重定向。这就是我正在做的事情:

       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
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      使用以下代码添加 global.asax 页面

      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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-13
        • 1970-01-01
        • 2011-08-07
        • 2010-12-16
        • 2020-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多