Centralised 404 error handling in ASP NET Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part 58 of asp.net core tutorial in this video well discuss how to handle 4:04 errors that is page not found errors in a centralized way in asp.net core along the way we'll also discuss these three middleware components that deal with status code pages in a spirit net core there are two types of 4:04 errors type 1 resource where the specified ID does not exist we discussed how to handle this type of 4:04 errors and display a more friendly and customized error view to the user in our previous video and here's the second type the URL does not match any road for example /foo slash bar this does not match with any route in our application in this video we'll discuss how to handle this type of 4:04 errors in a centralized way if we take a look at startup dot CS file it contains our startup class and this class has the configure method as discussed in our previous videos in this series it is this method that sets up the HTTP request processing pipeline for our asp.net core application at the moment we do not have anything configured within this HTTP request processing pipeline to hand out for zero for errors so as a result if we navigate to localhost colon port number / foo slash bar we see the browser default for zero for error page this is because this URL slash full slash bar does not match with any routes in our application notice the HTTP status code is for zero for these three status code pages middleware components i used to intercept and handle these non-success HTTP status codes in the range of 400 and 500 the least useful among these three is this use status code pages middleware component to use this with an or asp.net core application we have to configure it into the HTTP request processing pipeline we do that within the configure method so if the environment is development then we want to use the standard developer X in Page Mill where if it is any other environment like staging you ad production etc we want to use the stress code pages middleware component notice from the intellisense we see the three status code pages middleware components let's try the first one and see what we get at this point if we reload this page notice we still see the browser default for zero for page this is because at the moment our environment is still development let's change it to production we do that in launch settings dot JSON file so for this is Express profile let's change the a spinet core environment variable value to production if we reload this page now notice we see a simple text response status code four zero four not found this is not very different from the browser default four zero four page so for this reason we rarely use this use status code pages will be a component in a real world production application now what we really want to be able to do is intercept these non-success HTTP status codes like this four zero four for example and then return our own custom error view with our own view we have complete control over what HTML we want to send back to the user we can very easily achieve this by using any of these two mil web components let's first try out this status code pages with redirects component so in the configure method instead of using status code pages let's use status code pages with redirects with this middleware component we can specify the URL that we want to go to if there is a non success status code for example in our case we want to go to error controller we don't have error controller yet we'll create it in just a bit within the error controller we want to go to an action method that has the same name as the non-success HTTP status code for example if we have 4:04 status code then within this error controller we want an action method with named 4:04 to handle that for that I'm going to include a placeholder here this placeholder automatically receives the non-success status code for example if we have 4 0 4 then the URL will be slash error / 4 0 4 so our obvious next step is to add this error controller let's add it to the controller's folder select controller class and let's name it error controller we can name this action method anything we want I'm going to name it HTTP status code handler we are going to use attribute routing to get this action method if the route is error / some non success status code like 4 0 4 then we want this action method to be executed instead of hard-coding the status code like this let's use a placeholder now if there is a 4 0 for error this parameter will automatically receive that non-success status code for 0 4 and we want to pass that to this action method inside this method we can now write our own custom logic to handle 4 0 4 status code I'm going to use switch statement and switch on the status code parameter if the case is 4 0 4 on the viewbag object let's include error message property and set it to sorry the resource you requested could not be found finally let's use the break keyword and break out of this case statement at the moment as you can see we are only handling the 4:04 status code but you can see the pattern here we can write our own custom logic to handle any non success status codes in the range of 4 and 500 finally from this action method we want to return our custom view not found we don't have this view yet so let's add it to the shared folder select rays of you name it not found first let's say the page title for that let's use title property of the view bag object let's set page title to not found next let's display the aramis's that we have in this error message property of our view bag object on our custom view for that let's use an h1 element finally let's also provide a navigation link that allows the user to navigate to the home page here we're using the ASP - action and SP - controller tag helpers to navigate the user to the index section of our home controller at this point let's say all our changes and take a look at the browser notice now when we reload this page we see our custom error view when we click on this link we are redirected to the home page where we see the full list of employees if we try to navigate to any route that does not exist we see the custom error view at the moment we are using use status code pages with redirects malware to achieve this if we want to achieve the same using this Millbrae component then all we have to do is instead of dot CS instead of using status code pages with redirects middleware use status code pages with re execute middleware now let's click on this list navigation menu item we see the list of employees as expected now let's try to navigate to a route that does not exist notice even now we see our custom error view as expected from an end-user standpoint there's no difference in the behavior so the obvious question that comes to our mind is what's the difference between these two components and which one we should be using will answer these two questions in our next video three simple steps to use these status code pages middleware components to handle 4:04 errors step 1 include status code pages middle a component in your applications request processing pipeline we do this in the configure method of the startup class step to implement the error controller and finally implement the corresponding custom error view that's it in this video thank you for listening [Music] you [Music]
Info
Channel: kudvenkat
Views: 74,247
Rating: undefined out of 5
Keywords: asp.net core status code pages, asp.net core 404 handler, asp.net core mvc handle 404, asp.net core handle all 404, asp.net core use status code pages, asp.net core usestatuscodepages, usestatuscodepages asp.net core, usestatuscodepages not working, usestatuscodepages example, usestatuscodepages 404, usestatuscodepages .net core
Id: DVo138knAHQ
Channel Id: undefined
Length: 9min 43sec (583 seconds)
Published: Mon May 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.