


.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
- if (!IsPostBack)
- {
- HttpException erroy = new HttpException();
- string strCode = erroy.ErrorCode.ToString();
- string strMsg = erroy.Message;
- StringBuilder sb = new StringBuilder();
- sb.Append("-----------记录开始时间:" System.DateTime.Now "-----------------
"); - erroy.HelpLink = Request.QueryString["aspxerrorpath"];
- sb.Append("ErrorCode:" strCode "
"); - sb.Append("Message:" strMsg "
"); - sb.Append("HelpLink:" erroy.HelpLink "
"); - sb.Append("Source:" erroy.Source "
"); - sb.Append("TargetSite:" erroy.TargetSite "
"); - sb.Append("InnerException:" erroy.InnerException "
"); - sb.Append("StackTrace:" erroy.StackTrace "
"); - sb.Append("GetHtmlErrorMessage:" erroy.GetHtmlErrorMessage() "
"); - sb.Append("erroy.GetHttpCode().ToString():" erroy.GetHttpCode().ToString() "
"); - sb.Append("erroy.Data.ToString():" erroy.Data.ToString() "
"); - sb.Append("----------记录结束----------------");
- Response.Write(sb.ToString());
- }
到此为止:网站错误配置完成。当然错误处理页面你可以随意定义,你可以把捕捉到的错误写入数据库或者文件,只显示一些提示信息给用户,你也可以把错误信息处理后友好的显示给用户。
还有一种方法是在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
- protected void Application_Error(Object sender, EventArgs e)
- {
- System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");
- System.Web.Configuration.CustomErrorsSection customErrors = (System.Web.Configuration.CustomErrorsSection)config.GetSection("system.web/customErrors");
- if (customErrors != null && (customErrors.Mode == System.Web.Configuration.CustomErrorsMode.On || customErrors.Mode == System.Web.Configuration.CustomErrorsMode.RemoteOnly))
- {
- System.Web.HttpApplication app = (HttpApplication)sender;
- System.Exception lastError = app.Server.GetLastError();
- System.Web.HttpException httpEx = (HttpException)lastError;
- if (httpEx != null)
- {
- int httpErrorCode = httpEx.GetHttpCode();
- string redirect = customErrors.DefaultRedirect;
- foreach (System.Web.Configuration.CustomError error in customErrors.Errors)
- {
- if (error.StatusCode == httpErrorCode) redirect = error.Redirect;
- }
- app.Server.ClearError();
- app.Context.Response.StatusCode = httpErrorCode;
- Server.Transfer(redirect);
- }
- }
- }
这样问题就得以解决了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

If you encounter an error message when using your printer, such as the operation could not be completed (error 0x00000771), it may be because the printer has been disconnected. In this case, you can solve the problem through the following methods. In this article, we will discuss how to fix this issue on Windows 11/10 PC. The entire error message says: The operation could not be completed (error 0x0000771). The specified printer has been deleted. Fix 0x00000771 Printer Error on Windows PC To fix Printer Error the operation could not be completed (Error 0x0000771), the specified printer has been deleted on Windows 11/10 PC, follow this solution: Restart Print Spool

Table of Contents Solution 1 Solution 21. Delete the temporary files of Windows update 2. Repair damaged system files 3. View and modify registry entries 4. Turn off the network card IPv6 5. Run the WindowsUpdateTroubleshooter tool to repair 6. Turn off the firewall and other related anti-virus software. 7. Close the WidowsUpdate service. Solution 3 Solution 4 "0x8024401c" error occurs during Windows update on Huawei computers Symptom Problem Cause Solution Still not solved? Recently, the web server needs to be updated due to system vulnerabilities. After logging in to the server, the update prompts error code 0x8024401c. Solution 1

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

Title: Analysis of Oracle Error 3114: Causes and Solutions When using Oracle database, you often encounter various error codes, among which error 3114 is a relatively common one. This error generally involves database link problems, which may cause exceptions when accessing the database. This article will interpret Oracle error 3114, discuss its causes, and give specific methods to solve the error and related code examples. 1. Definition of error 3114 Oracle error 3114 pass

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

The display error is a problem that may occur in the Xiangxiang Fuzhai app. Some users are not sure why the Xiangxiang Fuzhai app displays errors. It may be due to network connection problems, too many background programs, incorrect registration information, etc. Next, This is the editor’s introduction to how to solve app display errors for users. Interested users should come and take a look! Why does the Xiangxiang Fuzhai app display an error answer: network connection problem, too many background programs, incorrect registration information, etc. Details: 1. [Network problem] Solution: Check the device connection network status, reconnect or choose another network connection to use. Can. 2. [Too many background programs] Solution: Close other running programs and release the system, which can speed up the running of the software. 3. [Incorrect registration information

LinuxOops: Detailed explanation of the meaning of this error, need specific code examples What is LinuxOops? In Linux systems, "Oops" refers to a situation where a serious error in the kernel causes the system to crash. Oops is actually a kernel crash mechanism that stops the system when a fatal error occurs and prints out relevant error information so that developers can diagnose and fix the problem. Oops usually occur in kernel space and have nothing to do with user space applications. When the kernel encounters

Title: How to configure and install FTPS in Linux system, specific code examples are required. In Linux system, FTPS is a secure file transfer protocol. Compared with FTP, FTPS encrypts the transmitted data through TLS/SSL protocol, which improves Security of data transmission. In this article, we will introduce how to configure and install FTPS in a Linux system and provide specific code examples. Step 1: Install vsftpd Open the terminal and enter the following command to install vsftpd: sudo
