-->

01 March 2020

custom Errors

  Asp.Net CS By Example       01 March 2020

 Asp.Net Custom Errors 

 In this article we are leanin about ASP.NET Custom Errors.for example when our web application has been deployed to a live web server, while accessing it sometimes things go terribly wrong. The Internet gets clogged, an application’s database server goes down, and so on. ASP.NET will report errors by displaying a message in the browser window.

 How do we recover from these types of errors gracefully in this situation? We have several options for avoiding this default experience: we can configure a custom error page; we can handle the error on the server side. Let’s examine each of these options.

The default error message that’s shown to remote users doesn’t contain code or other sensitive data, but we can customize the page that’s displayed to visitors when errors occur using a Web.config element called customErrors.

We define the customErrors element as a child of the system.web element like so:

Web.config
 <?xml version="1.0"?>
 <configuration>
  <system.web> 
   <customErrors mode="On" defaultRedirect="~/FrmErrorDefault.aspx" /> 
  </system.web>
 </configuration>

The defaultRedirect attribute of the customErrors element is used to specify the page that’s used to report errors. We can then choose whether this error page is shown to everybody, to nobody, or only to users who access the site from another network using the mode attribute.

The possible values for the mode attribute are:

Sr.No Mode Value Description
1) On When mode is On, ASP.NET uses user-defined custom error pages, instead of its default error page, for both local and remote users.
2) RemoteOnly When mode has the RemoteOnly value, the ASP.NET error page is shown only to local users, and the custom error page is shown to remote users. RemoteOnly is the default value, and is generally the safest option during development.
3) Off When mode has a value of Off, ASP.NET uses its default error page for both local and remote users. The customErrors element has no effect when mode is set to Off.

 In below Code show how to use Custom Error.

  Code: FrmErrorDefault.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmErrorDefault.aspx.cs" 
Inherits="LearnAsp.Net.ControlDemo.ApplicationState.FrmErrorDefault" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1 style="color:Red;">Error Message</h1>
    <h2 style="color:Orange;">OOPs Some thing happen wrong</h2>
    </div>
    </form>
</body>
</html>

  Code: FrmErrorPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmErrorPage.aspx.cs" 
Inherits="LearnAsp.Net.ControlDemo.ApplicationState.FrmErrorPage" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

  Code: FrmErrorPage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LearnAsp.Net.ControlDemo.ApplicationState
{
    public partial class FrmErrorPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            throw new Exception("Server Error"); 
        }
    }
}

  Output:
logoblog

Thanks for reading custom Errors

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.