Centralised 404 error handling in ASP NET Core











>> YOUR LINK HERE: ___ http://youtube.com/watch?v=DVo138knAHQ

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.blogsp... • 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. •    / @aarvikitchen5572   • Slides • https://csharp-video-tutorials.blogsp... • ASP.NET Core Text Articles Slides • https://csharp-video-tutorials.blogsp... • ASP.NET Core Tutorial •    • ASP.NET core tutorial for beginners   • Angular, JavaScript, jQuery, Dot Net SQL Playlists • https://www.youtube.com/user/kudvenka... • • 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} );

#############################









Content Report
Youtor.org / YTube video Downloader © 2025

created by www.youtor.org