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

Jun 24, 2016 pm 12:02 PM
Configuration mistake page

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

这样问题就得以解决了。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Unable to complete operation (Error 0x0000771) Printer error Unable to complete operation (Error 0x0000771) Printer error Mar 16, 2024 pm 03:50 PM

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

Solution to Windows Update prompt Error 0x8024401c error Solution to Windows Update prompt Error 0x8024401c error Jun 08, 2024 pm 12:18 PM

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

The working principle and configuration method of GDM in Linux system The working principle and configuration method of GDM in Linux system Mar 01, 2024 pm 06:36 PM

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

Interpreting Oracle error 3114: causes and solutions Interpreting Oracle error 3114: causes and solutions Mar 08, 2024 pm 03:42 PM

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

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

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

Why does the Xiangxiangfuzhai app display an error? Why does the Xiangxiangfuzhai app display an error? Mar 19, 2024 am 08:04 AM

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

Linux Oops: Detailed explanation of what this error means Linux Oops: Detailed explanation of what this error means Mar 21, 2024 am 09:06 AM

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

How to configure and install FTPS in Linux system How to configure and install FTPS in Linux system Mar 20, 2024 pm 02:03 PM

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

See all articles