Whenever we develop any asp.net application error occurs when
ever we perform some unwanted operation. So in this application I will show you
how you can develop a custom error page when ever some error occurs user will automatically
redirected to the custom error page in your ap.net application. In this
tutorial I will give an example by using C#.Net.
Some of my previous Asp.Net articles are as follows :
jQuery
DateTime Picker Calender In ASP.NET MVC | How To Access jQuery DateTime Picker
Calender Value in MVC Controller , Random
Row With LINQ To SQL Using C#.Net | Randomly Select Records Using Linq to SQL
Using C# , Merge
two datatables in C#.Net and VB.Net | How to merge multiple datatables using
C#.Net , How
to create a Xml Document Programmatically Using C#.Net .
Please have a look of eBooks of asp.net to free download: Free
Download ebook - Mobile ASP.NET MVC 5, Designing
Evolvable Web APIs with ASP.NET, Beginning
jQuery 2 for ASP.NET Developers - Free download ebooks .
So for this article first we will select our defaut.aspx page add the below mention code to throw the
exception :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class _Default : System.Web.UI.Page
{
/// <summary>
/// Custom error page resirect
/// Raising error by deviding value with 0
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
protected
void Page_Load(object
sender, EventArgs e)
{
int
x = 1;
int
y = 0;
int
value = x / y;
}
}
}
|
In above code we are dividing a number by zero, this
operation is not allowed in c#. so when we execute the page on that case we
will get below mention error.
Now we will put a custom error page. For this we will create
a custom error page(error.aspx) and make some changes in our web.config file.
<?xml version="1.0"?>
<!--
For more information on
how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="Error.aspx"/>
<compilation debug="true"/>
</system.web>
</configuration>
|
In above code we have added an error.aspx page this page
will be displayed when some error will occur. Now run the application. You will
get the below mention error and after error you will not get above mention
error page. You will be automatically redirected to error.aspx page.
Now your custom Error page will appear
0 comments:
Please let me know your view