Application level error handling can be done in 2 ways:1. Add an Application_Error event handler to the global asax file for your web application
2. Add a <customErrors> element in web.config file for your web application.
1. Application_Error Event handler(Using first way)
Code in .aspx file
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="page1].aspx.cs" Inherits="page1_" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <script runat="server">
- //btn1 click event and we want to transer to page welcome.aspx which
- //is does not exists but we want to check error so i assumed that i taken
- //welcome.aspx which is not in application you can specify any other file name
- //which is not in your applciaiotn
- protected void btn1_click(object s, EventArgs e)
- {
- Server.Transfer("welcome.aspx");
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>GLOBAL ASAX</title>
- </head>
- <body>
- <form id="form1" runat="server">
- taken a button on which when client clicks
- <asp:Button ID="btn1" runat="server" Text="Click here to check" OnClick="btn1_click" />
- </form>
- </body>
- </html>
After that add Global.asax file and place code showing below:
- <%@ Application Language="C#" %>
- <script runat="server">
- void Application_Start(object sender, EventArgs e)
- {
-
- }
- void Application_End(object sender, EventArgs e)
- {
-
- }
-
-
- void Application_Error(object sender, EventArgs e)
- {
-
-
-
-
- Response.Write(Server.GetLastError().Message);
- Server.ClearError();
- }
- void Session_Start(object sender, EventArgs e)
- {
-
- }
- void Session_End(object sender, EventArgs e)
- {
-
-
-
-
- }
- </script>
2. <customErrors element in web.config file(Second way)
Code in aspx page will be same as shown above in aspx file....
now add web.config file in your application and place code shown below:
- <?xml version="1.0"?>
- <configuration>
- <appSettings/>
- <connectionStrings/>
- <system.web>
-
-
- <!--Second is RemoteOnly in that case custom error page will not apply to visitors logged on locally
- at the web server itself.and its useful for developers as well for administrator for troubleshooting-->
- <!--In defaultRedirect attribute you specify custom error page that will display when any error occures i taken
- Error.htm so that if any error occure this htm page get invoked-->
- <customErrors defaultRedirect="Error.htm" mode="On"/>
- <compilation debug="false" />
- <authentication mode="Windows" />
- </system.web>
- </configuration>
Code in HTML file for Custom 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>
- <title>Error on Page</title>
- </head>
- <body bgcolor="white">
- <font color="red">Error while processing</font>
- </body>
- </html>
This html page get shown in case of error
Note:
If you add both web.config as well Global.asax file then Global.aspx will handle your error not web.config