Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to handle the exception of application program in ASP.NET project development

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, Xiaobian will bring you about how to handle exceptions in ASP.NET project development applications. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.

ASP.NET project development in the application exception handling what are the problems? Let us begin our presentation:

I believe that everyone is familiar with the Application object, and the Global.asax file has been added to the project. Indeed, ASP.NET code that handles exceptions at the application level is placed under Application_Error event handling in Global.asax:

void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs }

We can catch all exceptions in the event handling method above, and we can also log the exception to the log file and send an Email to tell the developer what happened, as follows

ASP.NET Project Development Application Exception Handling Code

Exception error = Server.GetLastError().GetBaseException(); //Log exception if (!) in event log EventLog.SourceExists("ApplicationException")) { EventLog.CreateEventSource("ApplicationException", "Application"); } EventLog eventLog = new EventLog(); eventLog.Log = "Application"; eventLog.Source = "ApplicationException"; eventLog.WriteEntry(error.ToString(), EventLogEntryType.Error); //Send Email to Developer MailMessage email = new MailMessage("administrator@xiaoyang.com", "vince. PoweredByV2.com"); email.Body = error.ToString(); email.Subject = "An error occurred in the Application"; SmtpClient smtpClient = new SmtpClient("127.0.0.1", 25); smtpClient.Send(email); Response.Redirect("ErrorPage.aspx");

Of course, for the above code to work correctly, we must add the corresponding namespace to Global.asax, and when sending emails, the above "127.0.0.1" must be changed to our own mail server address:

Another thing to note is that ASP.NET runs as an ASPNET account, which has limited permissions. If we want to make the above code work, we must give the ASPNET account access to the registry. If you don't give permission, then the code above will report an error.

We give ASPNET accounts access to the "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog" node and byte points.

ASP.NET project development in the application exception handling how to configure permissions:

1. Open the Run menu

2. Type "regedit" and then OK

3. Navigate to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog node.

4. Right-click on this node and select "Permission". The Permission Configuration window will pop up.

5. Click "Add", click "Advanced" in the pop-up window, then click "Find",*** Find "ASPNET" account in the window below, OK.

6. Give ASPNET account read permission and it's OK.

The above code, if we do not *** add the Response.Redirect method, after the error, the user will see the very classic yellow error page. We also know that the classic error page exposes a lot of information, so we often navigate to our custom error page.

ASP.NET project development in the application exception handling of the basic situation to introduce you here, hoping to help you understand ASP.NET project development in the application exception handling.

The above is how to handle exceptions in ASP.NET project development. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report