Exception Handling in Asp.NET Core 6.0

Exception Handling in Asp.NET Core 6.0

Exception Handling in Asp.NET Core 6.0

ASP.NET Core Exception Handling in Development and Production Environments.  

We can catch all exceptions by Adding a Dedicated Action Method
We can also add a common Action method which will be called everything some error occurs in our application.
We also want this common action method should only be called in Production environment.
1. Create an Action method in your Home Controller

public IActionResult Error() {
    return View();

2. Add a Razor View called Error inside the Views ➤ Home

<h2>Some Problem</h2>
<p>We got some problem, please visit back after sometime.</p>


3. update the program.cs for development and production environment

public class Program
    public static void Main(string[] args)
        var builder = WebApplication.CreateBuilder(args);
        var app = builder.Build();

        if (app.Environment.IsDevelopment()) {
        else {



Now what we want is that:
1. When the application is running in production environment the users will see a simple text message telling them there is
    some problem which will be rectified in a moment so come back after sometime.
2. When the application is running in Development environment then users will see full error details.

The UseExceptionHandler adds Middleware to catch exceptions occurring in our ASP.NET Core applications. The app.UseExceptionHandler(“/Home/Error”) sets the error path which is “/Home/Error”, here custom user friendly error messages can be displayed to the user

Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.