May 13, 2021 ASP.NET
There ASP.NET three aspects of exception handling in the ASP.NET:
In this chapter, we'll discuss tracing and handling. A nd in this chapter, we'll cover debugging.
To understand the concept, create the following sample application. I t has a label control, a dropdown list, and a link. T he dropdown list loads an array list of quotes and the selected reference appears in the label below. I t also has a hyperlink that points to a link that does not exist.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="errorhandling._Default" %>
<!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>
Tracing, debugging and error handling
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblheading" runat="server" Text="Tracing, Debuggin and Error Handling">
</asp:Label>
<br /> <br />
<asp:DropDownList ID="ddlquotes" runat="server" AutoPostBack="True" onselectedindexchanged="ddlquotes_SelectedIndexChanged">
</asp:DropDownList>
<br /> <br />
<asp:Label ID="lblquotes" runat="server">
</asp:Label>
<br /> <br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="mylink.htm">Link to:</asp:HyperLink>
</div>
</form>
</body>
</html>
Code after file:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[,] quotes =
{
{"Imagination is more important than Knowledge.", "Albert Einsten"},
{"Assume a virtue, if you have it not" "Shakespeare"},
{"A man cannot be comfortable without his own approval", "Mark Twain"},
{"Beware the young doctor and the old barber", "Benjamin Franklin"},
{"Whatever begun in anger ends in shame", "Benjamin Franklin"}
};
for (int i=0; i<quotes.GetLength(0); i++)
ddlquotes.Items.Add(new ListItem(quotes[i,0], quotes[i,1]));
}
}
protected void ddlquotes_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlquotes.SelectedIndex != -1)
{
lblquotes.Text = String.Format("{0}, Quote: {1}", ddlquotes.SelectedItem.Text, ddlquotes.SelectedValue);
}
}
}
To allow page-level tracking, you need to modify the Page directive and add a Trace property as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="errorhandling._Default" Trace ="true" %>
Now when you execute the file, you'll get tracking information:
It provides the following information in the first part:
Each time a page is needed, the status code from the server displays the name and error time, if any. The following table shows the normal HTTP status code:
Digital | Describe |
---|---|
Notifications (100 - 199) | |
100 | Go on |
101 | The conversion protocol |
Success (200 - 299) | |
200 | Ok |
204 | No content |
Redirect (300 - 399) | |
301 | Permanently move |
305 | Use a proxy |
307 | Temporarily redirect |
Errors from clients (400 - 499) | |
400 | The error request |
402 | Payment needs |
404 | Not found |
408 | The request timed out |
417 | The expectation fails |
Errors from the server (500 - 599) | |
500 | Internal server error |
503 | The service is not available |
505 | The HTTP version is not supported |
Under top-level information, there is a Trace log that provides details of the page lifecycle. I t provides the run time in seconds after the page is initialized.
The next section is the control tree, which lists all the controls on the page in a hierarchical form:
The final declaration in Session and Application follows a collection of summaries, cookies, and headers for all server variables.
Trace objects allow you to add custom information to trace outputs. I t does this in two ways: the Write method and the Warn method.
Change Page_Load event handle is detecting the Write method:
protected void Page_Load(object sender, EventArgs e)
{
Trace.Write("Page Load");
if (!IsPostBack)
{
Trace.Write("Not Post Back, Page Load");
string[,] quotes =
.......................
}
}
Run to see the impact:
To detect the Warn method, let's force some incorrect code into the selected index changed event handle:
try
{
int a = 0;
int b = 9 / a;
}catch (Exception e)
{
Trace.Warn("UserAction", "processing 9/a", e);
}
Try-Catch is a C-programming structure. T he try block holds any code that can or can't produce an error, and the catch block catches the error. W hen the program is running, it sends a warning in the trace log.
Application-level tracking is applied to all pages in the site. I t is implemented by putting the following code into the web.config file:
<system.web>
<trace enabled="true" />
</system.web>
Although ASP.NET can detect all run-time errors, there are still some minor errors out there. O bserving errors by tracking is for developers, not users.
Therefore, in order to prevent this from happening, you can add error resolution settings to the application's web.config. I t is application-wide error resolution. F or example, you can add the following code to the web.config file:
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
</system.web>
<configuration>
Some of the possible properties: - Mode: It allows or does not allow custom error pages. I
t has three possible values: - . -
show ASP.NET error page (yellow page) - s remoteOnly:, it shows custom errors to the client, showing local ASP.NET errors. -
defaultRedirect: It contains the URL of the page to display errors that cannot be resolved. I
n order to place different custom error pages for different error types, sub-labels are used, where different error pages are specified based on the wrong status code.
To achieve page-level error resolution, the Page directive can be modified to ASP.NET: ''''''''