Sabari M Sabari M
Updated date Dec 09, 2020
In this article, we will learn how to handle the exception in ASP.NET Core. Exception Handling is one of the essential features of programming applications. When errors occur in your ASP.NET Core application, we can easily handle the errors in different ways.

Exception Handling is one of the essential features of programming applications. When errors occur in your ASP.NET Core application, we can easily handle the errors in different ways. ASP.NET Core provides a middleware that helps exception handling very easy.

Microsoft.AspNetCore.Diagnostics  the package provides below middleware methods,

  1. UseDeveloperExceptionPage
  2. UseExceptionHandler

By default, this package is included in ASP.NET Core applications created via Visual Studio. If not, then you can install Microsoft.AspNetCore.Diagnostics package via the package manager.

Below example returns an exception when you run the application, 

namespace ASPNETCoreApp
{
    public class Startup
    {       
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {    
            // This line of code will throw an exception
            app.Run(context => { throw new Exception("exception"); });      
        }
    }
}

You will see the below screen with HTTP ERROR 500 status code when you run the above code,

 

UseDeveloperExceptionPage :

The UseDeveloperExceptionPage an extension method is used to display the developer-friendly detailed error information from the request pipeline. The developers can easily identify the errors during development.

namespace ASPNETCoreApp
{
    public class Startup
    {    
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                // This method is used to display the developer friendly detailed error informations from request pipeline
                app.UseDeveloperExceptionPage();
            }
            // This below code will throw an exception
            app.Run(context => { throw new Exception("custom exception"); });
        }
    }
}

When you run the above code you will see the detailed error information as shown below,

 

UseExceptionHandler:

The UseExceptionHandler an extension method is used to configure custom error handling route information and will redirect the custom error page when an error occurs in the ASP.NET Core application. This method can be used in the application which is running in the production environment.

The below example shows how to add the custom page to the UseExceptionHandler method, the UseExceptionHandler("/Error") sets the error handler path. If an error occurred in the application then will redirect to this Error page.

namespace ASPNETCoreApp
{
    public class Startup
    {     
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // This extension method is used to configure custom error handling route information.
            app.UseExceptionHandler("/Error");
        }
    }
}

Where do I use UseDeveloperExceptionPage and UseExceptionHandler methods?

UseDeveloperExceptionPage method should be used only for Development and Staging environments since it will display sensitive information related to the application code. So it is safer when we use this in Development or Staging.

UseExceptionHandler method should be used for the Production environment since we don't want to display sensitive information and displays only a custom error page to the user.

namespace ASPNETCoreApp
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // This UseDeveloperExceptionPage method should be used only in Development and Staging
            if (env.IsDevelopment() || env.IsStaging())
            {
                app.UseDeveloperExceptionPage();
            }
            else  // Production (other than Development and Staging)
            {
                app.UseExceptionHandler("/Error");               
            }          
        }
    }
}

 

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 14+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!