ASP.NET core : Working with Exception page

facebooktwittergoogle_plusredditpinterestlinkedinmail

Working with Exception page in ASP.NET core

Introduction to Developer Exception Page in ASP.NET core


In the previous version of ASP.NET we have a yellow error page which displays the stack trace and the errors while running the application. Do you like that page ? In my point of view its not that much developer friendly. Microsoft has redesigned that page with more information includes stack trace, query , cookies, headers and database exceptions etc. In this article explains about the brand new developer exception page in ASP.NET core.

To get this page , go to Startup.cs class . The Startup class is the entry point to the application, setting up configuration and wiring up services the application will use. We can configure a request pipeline in the Startup.cs class that is used to handle all the requests made to the application. Inside the Starup.cs class we have Configure method, which is used to specify how the ASP.NET application will respond to individual HTTP requests.

The Configure method accepts and IApplicationBuilder parameter , IHostingEnvironment and ILoggerFactory. In IHostingEnvironment we will get the current environment . However ,the error page is required only in development environments so in other environments we can disable the functionality. We have a developer exception page , runtime information page and database error page in place. Look at the following code snippet to enable developer exception page and database error page in Configure method. In the else statement you can see a separate exception handler for other environments.

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
 
            app.UseApplicationInsightsRequestTelemetry();
 
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage(); // To enable developer exception page.
                app.UseRuntimeInfoPage(); // To display runtime info page.
                app.UseDatabaseErrorPage(); // To enable database error page.
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
 
                // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
                try
                {
                    using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
                        .CreateScope())
                    {
                        serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
                             .Database.Migrate();
                    }
                }
                catch { }
            }
 
            app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
 
            app.UseApplicationInsightsExceptionTelemetry();
 
            app.UseStaticFiles();
 
            app.UseIdentity();
 
            // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715
 
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

Write some code which will throw the exception while running. Run the application. Look at the nice error page which has all the error details and error line highlighted in red color.

Developer Exception Page- Stack

Look at the header tab, which has all the request headers.

Developer Exception Page - Headers

Runtime information of an ASP.NET core application

You can see the method UseRuntimeInfoPage() inside the configure method, which will help us to get runtime information of an ASP.NET Core application.

Syntax: /runtimeinfo

runtimeinfo

In the preceding screenshot you can see all the runtime information like Operating system, runtime type , packages etc

Database error page of an ASP.NET core application

Database error page is to display the errors related to the database. By using this page you can even fix the database issue.

Database Error page

In the preceding screen there is an error from database side. It will be resolved by migrates the application context . Click on Apply migration.

Database Error page - Migration

Once we click on Apply migration the ASP.NET core automatically migrate ApplicationDbContext and that will fix the issue.

The following two tabs change content below.

Tom Mohan

Tom Mohan is a technologist who loves to code and build. He enjoys working on Microsoft Technologies. Tom specializes in ASP.NET MVC, Web API , Azure, C# ,WPF, SQL etc and holds a Bachelor engineering degree in Computer Science. Certification : MCSD , MCTS