Centralised 404 error handling in ASP NET Core



Centralised 404 error handling in ASP NET Core

Centralised 404 error handling in ASP NET Core

In this video we will discuss how to handle 404 errors i.e Page Not Found errors in a centralised way in asp.net core.

Text version of the video
https://csharp-video-tutorials.blogspot.com/2019/05/centralised-404-error-handling-in.html

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1

Slides
https://csharp-video-tutorials.blogspot.com/2019/05/centralised-404-error-handling-in_20.html

ASP.NET Core Text Articles & Slides
https://csharp-video-tutorials.blogspot.com/2019/01/aspnet-core-tutorial-for-beginners.html

ASP.NET Core Tutorial
https://www.youtube.com/playlist?list=PL6n9fhu94yhVkdrusLaQsfERmL_Jh4XmU

Angular, JavaScript, jQuery, Dot Net & SQL Playlists
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

Along the way, we will discuss the following 3 middleware components that deal with status code pages in asp.net core

UseStatusCodePages
UseStatusCodePagesWithRedirects
UseStatusCodePagesWithReExecute

Types of 404 errors

In ASP.NET Core there are 2 types of 404 errors that could happen

Type 1 : Resource with the specified ID does not exit. We discussed how to handle this type of 404 errors and provide a more meaningful and a more customised error view in Part 57 of ASP.NET core tutorial.

Type 2 : The provided URL does not match any route in our application. In this video, we will discuss how to handle this type of 404 error in a centralised way.

404 error example in ASP.NET Core

The following is code in Configure() method of Startup class in Startup.cs file. As you might already know, this Configure() method configures the HTTP request processing pipeline for our asp.net core application.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();

app.UseMvc(routes =]
{
routes.MapRoute(“default”, “{controller=Home}/{action=Index}/{id?}”);
});
}

Default 404 error page in ASP.NET Core

At the moment we do not have anything configured in this http request processing pipeline to handle 404 errors. So if we navigate to http://localhost/foo/bar, we see the browser default 404 error page. This is because the URL /foo/bar does not match any routes in our application.

Handling non-success http status codes

To handle non-success http status codes such as 404 for example, we could use the following 3 built-in asp.net core middleware components.

UseStatusCodePages
UseStatusCodePagesWithRedirects
UseStatusCodePagesWithReExecute

UseStatusCodePages Middleware

This is the least useful of the 3 status code middleware components. For this reason, we rarely use it in a real world production application. To use it in an application and see what it can do, plug it into the http processing pipeline as shown below.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseStatusCodePages();
}

app.UseStaticFiles();

app.UseMvc(routes =]
{
routes.MapRoute(“default”, “{controller=Home}/{action=Index}/{id?}”);
});
}

With UseStatusCodePages Middleware configured, if we navigate to http://localhost/foo/bar, it returns simple text response.

UseStatusCodePagesWithRedirects Middleware

In a production quality application we want to intercept these non-success http status codes and return a custom error view. To achieve this, we can either use UseStatusCodePagesWithRedirects middleware or UseStatusCodePagesWithReExecute middleware.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseStatusCodePagesWithRedirects(“/Error/{0}”);
}

app.UseStaticFiles();

app.UseMvc(routes =]
{
routes.MapRoute(“default”, “{controller=Home}/{action=Index}/{id?}”);
});
}

With the following line in place, if there is a 404 error, the user is redirected to /Error/404. The placeholder {0}, in “/Error/{0}” will automatically receive the http status code.

app.UseStatusCodePagesWithRedirects(“/Error/{0}”);

Comments are closed.