Home > Web Front-end > HTML Tutorial > .net three-step configuration error page to keep your website away from discordant pages_html/css_WEB-ITnose

.net three-step configuration error page to keep your website away from discordant pages_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:02:47
Original
1012 people have browsed it

If there are a bunch of incomprehensible error reports on your website, then you are not a qualified programmer or a qualified webmaster.

The following aspects can help your website avoid confusing pages.

Step 1: Configure web.config

Open web.config and add the following code under the node:

/>


Step 2: Create an error page

Create a 404 page in the root directory of the website (page not found) :404.html

403 page (server prohibited access): 403.html

This can solve part of the problem, but if there are bugs in our program and the user happens to find it, it will still return Give users an unfriendly error page. So we also need to create an ErrorPages.aspx to capture those error pages that we don't know about, to handle those error reports, and to display good pages to users.

Step 3: Capture unknown errors and display friendly prompts.

Add the following code to ErrorPages.aspx.cs:

[c-sharp]

view plain copy

  1. if (!IsPostBack)  
  2.        {  
  3.            HttpException erroy = new HttpException();  
  4.            string strCode = erroy.ErrorCode.ToString();  
  5.            string strMsg = erroy.Message;  
  6.            StringBuilder sb = new StringBuilder();  
  7.              
  8.            sb.Append("-----------记录开始时间:"   System.DateTime.Now "-----------------
    ");  
  9.            erroy.HelpLink = Request.QueryString["aspxerrorpath"];  
  10.            sb.Append("ErrorCode:"   strCode   "
    ");  
  11.            sb.Append("Message:"   strMsg   "
    ");  
  12.            sb.Append("HelpLink:"   erroy.HelpLink   "
    ");  
  13.            sb.Append("Source:"   erroy.Source   "
    ");  
  14.            sb.Append("TargetSite:"   erroy.TargetSite   "
    ");  
  15.            sb.Append("InnerException:"   erroy.InnerException   "
    ");  
  16.            sb.Append("StackTrace:"   erroy.StackTrace   "
    ");  
  17.            sb.Append("GetHtmlErrorMessage:"   erroy.GetHtmlErrorMessage()   "
    ");  
  18.            sb.Append("erroy.GetHttpCode().ToString():"   erroy.GetHttpCode().ToString()   "
    ");  
  19.            sb.Append("erroy.Data.ToString():"   erroy.Data.ToString()   "
    ");  
  20.            sb.Append("----------记录结束----------------");  
  21.   
  22.            Response.Write(sb.ToString());  
  23.        }  

到此为止:网站错误配置完成。当然错误处理页面你可以随意定义,你可以把捕捉到的错误写入数据库或者文件,只显示一些提示信息给用户,你也可以把错误信息处理后友好的显示给用户。

 

还有一种方法是在Global.asax中的void Application_Error(object sender, EventArgs e)方法中定义;现给以大体方法,具体操作可以根据实际情况给以修改。

在Global.asax文件中修改:

void Application_Error(object sender, EventArgs e)
{
//Code to run when an unhandled error occurs
Exception erroy = Server.GetLastError();
string err = " The error page is: " Request.Url.ToString() "
";
     err = "Exception message: " erroy.Message "
";
      err = "Source:" erroy.Source "
";
err = "StackTrace:" erroy.StackTrace "
";
//Clear the previous exception
//Server.ClearError() ;
                   //This processing uses Session["ProError"] to make an error. So use Application["ProError"]
Application["erroy"] = err;
//This is not in the page, so you cannot use Response.Redirect("../ErrorPages.aspx");
System.Web.HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath "/ErrorPages.aspx");

}

Modify in the ErrorPages.aspx.cs file

protected void Page_Load(object sender, EventArgs e)
{
//Display the error code in the program

if (!IsPostBack)
>                                                                                                                         String( ); Those that should return 404 will also return 302. This is very detrimental to search engine optimization. So we should add the following code in the Global.asax file:


[c-sharp]

view plain copy

  1. protected void Application_Error(Object sender, EventArgs e)  
  2.        {  
  3.            System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");  
  4.            System.Web.Configuration.CustomErrorsSection customErrors = (System.Web.Configuration.CustomErrorsSection)config.GetSection("system.web/customErrors");  
  5.            if (customErrors != null && (customErrors.Mode == System.Web.Configuration.CustomErrorsMode.On || customErrors.Mode == System.Web.Configuration.CustomErrorsMode.RemoteOnly))  
  6.            {  
  7.                System.Web.HttpApplication app = (HttpApplication)sender;  
  8.                System.Exception lastError = app.Server.GetLastError();  
  9.                System.Web.HttpException httpEx = (HttpException)lastError;  
  10.                if (httpEx != null)  
  11.                {  
  12.                    int httpErrorCode = httpEx.GetHttpCode();  
  13.                    string redirect = customErrors.DefaultRedirect;  
  14.                    foreach (System.Web.Configuration.CustomError error in customErrors.Errors)  
  15.                    {  
  16.                        if (error.StatusCode == httpErrorCode) redirect = error.Redirect;  
  17.                    }  
  18.                    app.Server.ClearError();  
  19.                    app.Context.Response.StatusCode = httpErrorCode;  
  20.                    Server.Transfer(redirect);  
  21.                }  
  22.            }  
  23.        }  

这样问题就得以解决了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template