Home >Web Front-end >HTML Tutorial >.net three-step configuration error page to keep your website away from discordant pages_html/css_WEB-ITnose
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
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
到此为止:网站错误配置完成。当然错误处理页面你可以随意定义,你可以把捕捉到的错误写入数据库或者文件,只显示一些提示信息给用户,你也可以把错误信息处理后友好的显示给用户。
还有一种方法是在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
这样问题就得以解决了。