create custom error page for exception handling in asp.net mvc
General
To create a custom error page in ASP.NET MVC that is displayed when an exception is thrown and caught by your code, you can use exception handling in combination with the HandleErrorAttribute attribute. Here are the steps to follow:
Create a new view file for your custom error page. For example, you might create a file called Error.cshtml in the Views/Shared folder.
In your Web.config file, add the following lines to the <system.web> section:
Exact Code
<customErrors mode="Off"/>
This will disable the default error handling behavior in ASP.NET MVC.
Add the [HandleError] attribute to your controller class or to individual controller actions that should handle exceptions. For example:
Exact Code
[HandleError]
public class HomeController : Controller
{
// ...
}
In your Global.asax.cs file, override the Application_Error method and add code to log the error and redirect to the custom error page. For example:
Exact Code
protected void Application_Error()
{
Exception exception = Server.GetLastError();
// Log the exception here, if desired
Response.Clear();
Server.ClearError();
Response.RedirectToRoute("Default", new { controller = "Error", action = "Index" });
}
This code clears the response, clears the error, and then redirects to the Error controller's Index action.
In your custom error page, you can access the exception details using the Model property. For example, you might display the exception message like this:
Exact Code
<h2>An error occurred:</h2>
<p>@Model.Exception.Message</p>
With these steps, any exceptions that occur in your application will be caught by the HandleError attribute and redirected to the custom error page. You can customize this page to display helpful information about the error, or to include links to additional resources for troubleshooting.
To create a custom error page in ASP.NET MVC that is displayed when an exception is thrown and caught by your code, you can use exception handling in combination with the HandleErrorAttribute attribute. Here are the steps to follow:
Create a new view file for your custom error page. For example, you might create a file called Error.cshtml in the Views/Shared folder.
In your Web.config file, add the following lines to the <system.web> section:
Exact Code
<customErrors mode="Off"/>
This will disable the default error handling behavior in ASP.NET MVC.
Add the [HandleError] attribute to your controller class or to individual controller actions that should handle exceptions. For example:
Exact Code
[HandleError]
public class HomeController : Controller
{
// ...
}
In your Global.asax.cs file, override the Application_Error method and add code to log the error and redirect to the custom error page. For example:
Exact Code
protected void Application_Error()
{
Exception exception = Server.GetLastError();
// Log the exception here, if desired
Response.Clear();
Server.ClearError();
Response.RedirectToRoute("Default", new { controller = "Error", action = "Index" });
}
This code clears the response, clears the error, and then redirects to the Error controller's Index action.
In your custom error page, you can access the exception details using the Model property. For example, you might display the exception message like this:
Exact Code
<h2>An error occurred:</h2>
<p>@Model.Exception.Message</p>
With these steps, any exceptions that occur in your application will be caught by the HandleError attribute and redirected to the custom error page. You can customize this page to display helpful information about the error, or to include links to additional resources for troubleshooting.
Share This with your friend by choosing any social account
You may also read following recent Post
![]() |
$1,000 cpc keywords
79 By Junaid A |
![]() |
How to grow 1% every day
119 By Junaid A |
![]() |
The setup for the installation of visual studio is not complete. Please run the Visual Studio installer again to correct the issue.
99 By Junaid A |
![]() |
Getting error "Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine"
231 By Junaid A |
![]() |
CRUD in Asp.net MVC
127 By Ihsan Ullah |
![]() |
What is life?
129 By Shoaib Khan |
![]() |
paragraph
135 By Ihsan Ullah |
![]() |
life
104 By khubaib Ahmad |
![]() |
How to start PMP
675 By Junaid A |
![]() |
what is robots.txt
664 By Junaid A |
![]() |
What is CURL?
714 By Junaid A |
![]() |
615 By shaheer |
![]() |
631 By shaheer |
![]() |
MVC LEARNIG
258 By |
![]() |
The data connection could not be established: ETIMEDOUT - Connection attempt timed out Error: Connec
262 By |
![]() |
show hide kendo ComboBox
275 By |
![]() |
Active A Class In Layout
290 By |
![]() |
Issues opening Excel documents - File format and extension of 'orders.xls' don't match. The file cou
242 By |
![]() |
Multiple errors occurred during the operation, the first of which is displayed below. A full error l
242 By |
![]() |
asp.net mvc routing tricks
254 By |
![]() |
What is routes in AngularJS?
282 By |
![]() |
Attackers might be trying to steal your information from (for example, passwords, messages, or cred
252 By |
![]() |
LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this
289 By |
![]() |
how to call a function in c#
302 By |