How to use global.asax

xiaoxiao2021-03-06  44

When the page encounters an unprocessed exception, it will be ugly information. In fact, as long as you add a code for processing an exception in the Application_Error event of the Global.asax file, you can avoid the user see those error information.

E.g

Add from the page_load event of WebForm1.cs

Using system.text.regularExpressions;

String errMessage = "If you encounter an unprocessed exception, please notify the administrator, thank you!"; if (session ["errmessage"]! = null && int.parse (session ["admin"])! = 0) // Session ["admin"] = 1 indicates that the viewer is an admin equipment errmessage = formaterrror (session ["errmessage"]. TOSTRING ());

Label1.text = errmessage;

Private string formaterror (string infutstring) {string temp = inputstring;

Temp = regex.replace (TEMP, "/ N", "
");

Return Temp;

Add in the Application_ERROR event of Global.asax

Exception lasterror = server.getlasterror (); session ["errmessage"] = lasterror.tostring ();

Server.Transfer ("Error.aspx"); below is specific to describe how to use the ASP.NET GLOBAL.ASAX file using Global.asax

转载请注明原文地址:https://www.9cbs.com/read-76362.html

New Post(0)