In this article, we will look at how to manually customize an error page in ASP.NET MVC 5.
MCV Introduction
The MVC architectural pattern separates the user interface (UI) of an application into three main parts.
Model
A set of classes that describes the data you are working with as well as the business logic.
View
Defines how the application’s UI will be displayed. It is pure HTML, which decides how the UI is going to look.
Controller
A set of classes that handles communication from the user, overall application flow, and application-specific logic
When an HTTP request arrives for an MVC application, that request gets routed to a controller, and then it is up to the controller to talk to either the database, the file system, or the model.
Why do we need a Custom Error Page?
We would just configure our custom error pages in one place and it would just work, no matter how/where the error was raised. In order to handle exceptions thrown by your action methods, you need to mark your method with the HandleError attribute. The HandleError attribute also allows you to use a custom page for this error.
Steps to be Followed
Step 1
Create a MVC application named “Http500Test”. For this demo, I have used VS 2019 and .NET version 4.7.2


The solution looks like this
Step 2
Create a Controller class file named “CustomerErrorUserController.cs”

[HandleError] attribute provided built-in exception filters. This attribute can be applied over the action method as well as the controller or at the global level.
At CustomerErrorUser/Index, this application will show error.cshtml as the view not found in Index but in the web.config file. The error statusCode="404" means that the file is not found and it means the controller name or controller action method name is the issue.
[HandleError] //If I put in this attribute and run, it will go to the Customized by a Default error page.
Step 3
Create a controller class named “ErrorController.cs”

I have added two action methods – NotFound() and UnhandledException().











Join the conversation! Your thoughts help the community grow.