Part 71. How to handle 404 errors in ASP NET Core MVC. | ASPNETCOREMVC.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] foreign [Music] Concepts my name is Amit and this is part 71 of the asp.net core MVC tutorial in this video we will learn how to handle 404 or similar errors in asp.net core MVC this is a continuation of part 70 add scoped at transient and at Singleton services so please watch it before proceeding to this one I shared the link in the description let's go to visual studio and this is the project which we are working on in part 68 of this video series we learned about the Hues exception Handler middleware along with developer exception page we have seen if we don't want to display the developer exception page or environments other than development we can redirect the user to any custom page with the help of use exception hander here we use this middle pair and where we called home controller and error action method let's see how it will behave in case of an invalid URL request let's run the program let's try to navigate to any page which does not exist in our application let's navigate to Super adminth we don't have any super admin controller so we get the 404 error page I was expecting use exception hander middleware will handle this error but that's not the case why because the use exception Handler middleware in asp.net core is designed to catch unhandle exceptions that occur during request processing and handle them in a centralized way however it may not catch 404 error because of 404 error is not technically an exception so we need to find some alternatives to deal with such errors let's see what are the different options we have so the first options which we can opt for is use status code pages by default and asp.net core app doesn't provide a status code page for HTTP error status codes such as 404 not found when the app sets an HTTP 400 to 599 error status code that doesn't have a body it Returns the status code and an empty response body to enable default text only handlers for common error status codes call use status code pages when use status code Pages isn't used navigating to a URL without an endpoint returns a browser dependent error message indicating that endpoint can't be found when used status code pages is called the browser Returns the following message you know status code 404 not found call use status code Pages before request handling middleware for example call use status code Pages before the static files middleware and the endpoint middleware use status code Pages isn't typically used in production environment because it returns a message that isn't useful to users there is an important note from Microsoft the status code pages middleware does not catch exceptions to provide a custom error handing page use the exception Handler page let's go to the visual studio and let's use the app dot use status code pages before use static files as described in our slide and then let's run our application let's navigate to super adventure map and we have a different response status code 404 not found this is what we described in our slide right next is use status code pages with the format string now if you have to customize the response content type and text we can use the overloaded method of use status code pages that takes a content type and format string let's try that as well let's go to the official Studio stop the application and type app dot use status code pages then text Dot plane and let's say status code page with formatted string colon within curly braces zero here curly braces 0 is a placeholder for the error code so whatever error we get we get the required error code here again Hue status code pages with the format string aren't typically used in production environment because it returns a message that isn't useful to user all right let's run our application again browse to any page let's say no controller slash no action and we get our custom message status code page with formatted string 404 04 here is our status code which we get within our placeholder next is use status code pages with Lambda now to specify custom error handling and response writing code use the overload of use status code pages that takes a Lambda expression let's see that in action let's go to visual studio and stop the application and type app.use status code Pages then async context such that for Lambda expression curly braces and close this statement if context dot HTTP context dot response dot status code is equal to status codes dot status 404 not found here we are writing our customize code 404 let's comment generate a custom 404 page so next context dot HTTP context dot response dot content type is equal to Text slash HTML await context dot HTTP context Dot response dot write async then HTML tag body tag H1 tag and let's say page not found then close H1 body and HTML tag next just copy and paste the if statement and append else change status 404 not found to status 500 internal server error within the command change 404 to 500 change the message from page not found to internal server error and that's it our use status code pages will take care of four zero four and five hundred on a similar note you can customize your code for other errors as well let's run our application and again use status code pages with Lambda isn't typically used in production because it returns a message that isn't useful to user browse to ABC slash XYZ see working as expected we are getting the required HTML format let's go to visual studio and stop the application now in a live project we don't want to display such a dummy message to the end users we want to greet them with our custom error pages where we can take advantage of our layout view as well just like we did with use exception Handler here we route the user to home controller and index action method so to achieve a similar thing for exception with codes between four zero zero and two five double line we can use use status code pages with redirects and use status code pages with re-execute let's use the use status code pages with redirect first so app dot use status code pages with redirects you can see it is expecting location format so make sure you specify the routes starting with a slash so let's say our route is status code error then we also want to capture the error code similar to what we discussed in previous cases curly braces 0 is a placeholder for status code so 2 0 within curly braces and that's it let's create one action method for this request go to the home controller copy and paste the already created action method we want to capture the status code so create one parameter in status code let's use action routing so status code error then slash create placeholder here so within the calipers status code so this action method is ready to receive requests for exceptions and we will get the status code within this parameter here it's up to you how you want to play with it let's apply one if condition if status code double equal to 404 then pass a custom message with the help of view back so viewbag dot error message is equal to 404 page not found exception let's create one view for this action method let's open the error view that we already created copy and paste the code now instead of generic message we want to display message received from our controller so at viewback dot error message go to the home controller and pass index as a view here we are only dealing with 404 you can apply as many checks as you want and you can create a separate views for separate error Pages if you want all right let's run our application navigate to slash ABC as you can see we get our custom error page and here we have all menus available because of our layout Pages simple let's use another option which is use status code pages with re-execute go to visual studio and instead of use status code pages with redirects use use status code pages with re-execute nothing else everything is similar let's run our code navigate to slash ABC as you can see it has similar output no change from the end user point of view there is no difference but for developers or the SEO both behave differently that we will discuss in our next video so that's it from this video hope you like our content if you have any queries related to the content of this video do ask me in the comments till then thanks for watching foreign [Music]
Info
Channel: PRO Concepts
Views: 1,149
Rating: undefined out of 5
Keywords: asp.net core, asp.net core tutorial, asp net mvc tutorial, dotnet core, dotnet core mvc, asp.net mvc core tutorial, asp.net mvc core course, dot net core tutorial, dot net core mvc tutorial, .net core, .net core tutorial, asp.net core return not found, asp.net core 404 not found, asp.net core 2 custom 404 page, handle 404 error in asp.net core, asp.net core catch 404, asp.net core 404 exception, asp.net core 404 error, asp.net core 404 handler, asp.net core redirect on 404
Id: 12clCUMPlsY
Channel Id: undefined
Length: 14min 15sec (855 seconds)
Published: Sat Mar 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.