• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Thursday, August 20, 2020

Developer Exception Page Middleware in ASP.NET Core

 Admin     August 20, 2020     .Net, .Net Core, Asp.Net, C#     No comments   

In this article, I am going to discuss how to handle an unhandled exception using Developer Exception Page Middleware in ASP.NET Core application. Please read our previous article where we discussed how to serve static files using Static Files Middleware in ASP.NET Core Web Application. The exception handling is one of the key features of any application. We can handle the exception in many different ways. But in this article, we are going to discuss how we can use the Developer Exception Page Middleware to handle the unhandled exception. As part of this article, we are going to discuss the following concepts.
  1. What is Developer Exception Page Middleware?
  2. How to use Developer Exception Page Middleware in ASP.NET Core Application?
  3. How to Customize the UseDeveloperExceptionPage Middleware in ASP.NET Core?
  4. Where do we need to configure the UseDeveloperExceptionPage Middleware?
Understanding Developer Exception Page Middleware:
By default, the ASP.NET Core application simply returns a status code for an exception that is not handled by the application. Let us understand this with an example. Please modify the Configure() method of the startup class as shown below where we throw an exception.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Run(async (context) =>
    {
        throw new Exception("Error Occurred while processing your request");
        await context.Response.WriteAsync("Request handled and response generated");
    });
}
When you run the application you will get the following output.
Developer Exception Page Middleware in ASP.NET Core application Error Status Code

As you can see in the above image it gives you the status code as 500 which means Internal Server Error. But as a developer when you are developing the application, you should have to know the detailed information about the exception on the page so that you can take necessary actions to fix the error.

How to use DeveloperExceptionPage Middleware in ASP.NET Core Application?
If you want your application to display a page that shows the detailed information about the unhandled exception, then you need to use the Developer Exception Page middleware. Let us modify the Configure() method as shown below to add the Developer Exception Page middleware which will handle the unhandled exception that occurred in our application.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.Run(async (context) =>
    {
        throw new Exception("Error Occurred while processing your request");
        await context.Response.WriteAsync("Request handled and response generated");
    });
}
With the above change, now run the application and it will display the following page with the detailed information about the unhandled exception.
Developer Exception Page Middleware in ASP.NET Core application

As you can see in the above image, the Developer Exception Page contains four tabs such as Stack, Queue, Cookies, and Headers. The Stack tab gives the information of stack trace which indicated where exactly the exception occurred, the file name and the line number that causes the exception. Query tab gives information about the query strings. The Cookies tab displays the information about the cookies set by the request and the Header tab gives the information about the headers which is sent by the client when makes the request.

Now if you verify the Query tab and Cookies tab, then you will not see any information as you are not passing any query string value in URL or you are not setting the cookies in the request. In our upcoming articles, we will discuss the Query string and Cookies in detail.

Note: Please Enable the Developer Exception Page Middleware only when the application is running in the Development environment. You don’t want to share detailed exception information when the application is running in the production environment.

How to Customize the UseDeveloperExceptionPage Middleware in ASP.NET Core?
If you want then you can also customize the UseDeveloperExceptionPage middleware. The point that you need to remember is whenever you want to customize a middleware component in ASP.NET Core then you need to use the respective Options object. For example
  1. UseDeveloperExceptionPage => to customize this middleware use DeveloperExceptionPageOptions object
  2. UseDefaultFiles => to customize this middleware use DefaultFilesOptions object
  3. UseStaticFiles => to customize this middleware use StaticFileOptions object
  4. UseFileServer => to customize this middleware use FileServerOptions object
As we are going to customize the UseDeveloperExceptionPage() middleware component, so we need to use the DeveloperExceptionPageOptions object as shown below.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        DeveloperExceptionPageOptions developerExceptionPageOptions = new DeveloperExceptionPageOptions
        {
            SourceCodeLineCount = 5
        };
        app.UseDeveloperExceptionPage();
    }
    app.Run(async (context) =>
    {
        throw new Exception("Error Occurred while processing your request");
        await context.Response.WriteAsync("Request handled and response generated");
    });
}
The SourceCodeLineCount property of the DeveloperExceptionPageOptions class specifies the number of lines of code to include before and after the line of code that caused the exception.

Where do we need to configure the UseDeveloperExceptionPage Middleware?
We need to configure the UseDeveloperExceptionPage() Middleware as early as possible in the application’s request processing pipeline so that it can handle the unhandled exception and then display the Developer Exception Page with the detailed information about the exception.

Let us see what happened when we configure the UseDeveloperExceptionPage() middleware after the middleware using is Registered using the Run() method. Please modify the Configure() method as shown below.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Run(async (context) =>
    {
        throw new Exception("Error Occurred while processing your request");
        await context.Response.WriteAsync("Request handled and response generated");
    });
    if (env.IsDevelopment())
    {
        DeveloperExceptionPageOptions developerExceptionPageOptions = new DeveloperExceptionPageOptions
        {
            SourceCodeLineCount = 5
        };
        app.UseDeveloperExceptionPage();
    }
}
With the above changes when we run the application, it will not display the developer’s exception page instead of simply returns the default error status code. This is the reason why we need to configure the UseDeveloperExceptionPage() middleware as early as possible to handle the unhandled exception of the application in the request processing pipeline.

From the next article onwards, I am going to discuss ASP.NET Core MVC Application. In this article, I try to explain how to handle an unhandled exception using Developer Exception Page Middleware in ASP.NET Core application. I hope this article will help you to understand the Developer Exception Page Middleware.

Summary:
I hope this post will be helpful to understand the concept of Developer Exception Page Middleware in ASP.NET Core
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape