Click here to Skip to main content
15,884,836 members
Articles / Web Development / ASP.NET / ASP.NET Core

ASP.NET Core 2.0 Error Pages

Rate me:
Please Sign up or sign in to vote.
1.67/5 (3 votes)
1 Sep 2017CPOL 5.5K   2
How to serve error pages in ASP.NET Core application. Continue reading...

Problem

How to serve error pages in ASP.NET Core application.

Solution

Starting from an empty project, created in a previous post, amend the Configure() method of Startup class to use middleware needed for error handling. Below, I’ve used a custom middleware (defined as lambda) to handle production exceptions:

C#
public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        var feature =
                            context.Features.Get();
                        var exception = feature.Error;
                        await context.Response.WriteAsync(
                            $"<b>Oops!</b> {exception.Message}");
                    });
                });
            }

            app.Run(async (context) =&gt;
            {
                throw new ArgumentException("T must be set");
                await context.Response.WriteAsync("Hello Error Handling!");
            });
        }

Alternatively, you could point to MVC controller/action, which would return a ViewResult:

C#
app.UseExceptionHandler("/Error");

Discussion

It is really simple to configure middleware to setup error pages for both development and production environment.

The error page for development environment shows all the relevant details that developers require to resolve the issue. However, this page is not suitable for production/end-user, it gives too much information which could be used for malicious reasons.

There are two ways in which we can show user and production friendly error pages:

  1. Plug-in a middleware into UseExceptionHandler (which itself is a middleware). This gives a lot of flexibility on how to build an error response.
  2. Provide path to MVC controller/action and create error page using MVC views. Note that the path must starts with ‘/’.
    • It is better to use only static content in error pages, to avoid them throwing exception.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
SuggestionBad Tagging Pin
Paul Selormey9-Sep-17 4:56
Paul Selormey9-Sep-17 4:56 
Why is this tagged as C++ and MFC?
Jesus Christ is LOVE!

GeneralRe: Bad Tagging Pin
User 10432649-Sep-17 5:17
User 10432649-Sep-17 5:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.