Home  >  Article  >  Web Front-end  >  Create a simple error handling page in a web project_html/css_WEB-ITnose

Create a simple error handling page in a web project_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:49:56995browse

When an error occurs in the application, if the error page is not processed, some sensitive information will be output directly, and sometimes the physical path where the project is located will be directly displayed, which seriously lacks security. , and there are many types of errors and different page styles, resulting in poor user experience. This article introduces how to create error pages in web projects to prompt error messages in a friendly manner.

1. Create the error page error.aspx. The front-end code is as follows (elements can be added according to actual needs):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="error.aspx.cs" Inherits="MES.Web.error" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <style type="text/css">        .style1        {            width: 74px;        }        .style2        {            width: 79px;        }    </style></head><body>    <form id="form1" runat="server">    <div align="center">        <table style="height: 85px; width: 657px;">            <tr>                <td>                    <img alt="system error" src="Images/errorpage.jpg" />                </td>            </tr>            <tr>                <td class="style2">                    <asp:Label ID="lblMessage1" runat="server" Font-Names="微软雅黑" Text=""></asp:Label>                </td>            </tr>            <tr>                <td class="style2">                    <asp:Label ID="lblMessage2" runat="server" Font-Names="微软雅黑" Text=""></asp:Label>                </td>            </tr>            <tr>                <td class="style2">                    <asp:Label ID="lblMessage3" runat="server" Font-Names="微软雅黑" Text=""></asp:Label>                </td>            </tr>        </table>    </div>    </form></body></html>


2. Process it globally Add error handling code to Application_Error in the file, as follows:

// 在出现未处理的错误时运行的代码            Exception objErr = Server.GetLastError().GetBaseException();            string sError = "异常页面:" + HttpContext.Current.Request.Url.ToString() + "异常信息:" + objErr.Message + "处理方法:请刷新页面重试或联系系统管理员。";                        //清除之前的异常            Server.ClearError();            //这里如果用Session["ProError"]会出错,所以用 Application["AppError"]            Application["AppError"] = sError;            //这里不能用Response.Redirect            System.Web.HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/error.aspx");


Third, when the error page is initialized, output the error message, as follows:

if (!IsPostBack)            {                if (Application["AppError"] != null)                {                    try                    {                        string msg = Application["AppError"].ToString();                        msg = msg.Replace("\"", "");                        lblMessage1.Text = msg.Substring(0, msg.IndexOf("异常信息"));                        lblMessage2.Text = msg.Substring(msg.IndexOf("异常信息"), msg.IndexOf("处理方法") - msg.IndexOf("异常信息"));                        lblMessage3.Text = msg.Substring(msg.LastIndexOf("处理方法"));                    }                    catch (Exception ex)                    {                        lblMessage1.Text = ex.Message;                    }                }            }


The above steps can complete the configuration of the error page. In addition, the error page can also be configured through Web.Config. The preview effect is as follows:

Statement:
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