How to handle 404 errors in ASP.NET Core MVC?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello youtube viewers hope everyone is doing well this video is about how to handle 404 errors in asp.net core mvc asp.net core mvc is the net core counterpart of asp.net mvc framework for building cross platform scalable high performance web applications and apis using model view controller design pattern surprisingly although asp.net core provides plenty of options for handling 404 errors gracefully the asp.net core mvc runtime does not take advantage of them by default as a result when a web page is not found and a 404 error is returned by the application asp.net core mvc presents only a generic browser error page this video discusses three options in asp.net core be used to handle 404 errors by more gracefully let's create an asp.net core mvc project in visual studio open visual studio 2022 and click on create a new project and search for templates type asp.net for web app and then select asp.net core web app model view controller click on next give project a meaningful name handle four zero four error you'll leave the location default and the solution name same as the project name click on next uncheck configure for https click on create we will use this project to illustrate our 404 error handling options when you execute the asp.net core mbc project we have created you'll see home page of the application with welcome message let's run the project as you can see the home page of the application is displayed with the welcome message now let's try to browse to a page that doesn't exist in the address bar type forward slash customers when the asp.net core mvc engine fails to locate the resource for the specified url if 404 error will be returned and you are presented with a not very elegant page can be found per page there are several ways in which you can improve on this generic error page a simple solution is to check for http status code 404 in the response if found you can redirect the control to a page that exists let's implement it close the browser to stop the execution of the app go to program.cs right under if blog type app.use brackets and async and then brackets again context command next then lambda expression and prices semicolon at the after the bracket and await next weight next and then if log check contact start response dot status code equals 404 then insert the if block redirect to contacts dot request dot path the page that exists let's redirect it to home page slash privacy home slash privacy not the home page and then wait text wait it has to be double equals save and let's run project and let's go to the same customers page and as you can see the control is redirected to privacy policy page this is one way of handling 404 errors a second solution for handling 404 errors in asp.net core is by using built-in use status code pages middleware that's implemented close the browser to stop the execution of the application and comment the existing code be implemented and add app dot use status code pages save it let's run it and let's go to customers page which doesn't exist as you can see a status code 404 dot font is displayed and that's another way of handling 404 errors in asp.net core mvc another solution to handle 404 errors in asp.net core mvc is by taking advantage of use status code pages with re-execute middleware let's implement the solution let's stop the execution of application by closing the browser this solution requires changing a few files so let's go and add a new property to our model we'll add a string property here public string error message and string dot empty let's initialize it it let's go and make changes to error.csstml we'll add a new vr tag here to get a new line and then strong tag and we'll display the error message we just added and the code tag will display the error message at model dot message it now let's go to home controller if there is an issue let's fix this okay go to home controller and add a new action method here which will be route slash home slash error that's code as an integer it would be public by action result error and accept code and we'll return view go pause new error remodel at the property request id test and error message equals we'll do use interpolation string interpolation better third error code is code save it now let's go to program.cs file add comment the existing code which we implemented in the last solution and add app.use with re-execute redirect and let's redirect it to flash home slash error parameter save it and let's execute it and let's go to customers which doesn't exist and as you can see we see the error this new message here error occurred and a record is for a four so that's another way of handling four or four errors uh in asp.net core mvc and finally you can create custom views for specific error codes for example you might create views such as home slash error slash 500.cs html you could then check the http error code and redirect to the appropriate page that's all for this video i hope this video was helpful and if you find this video useful please subscribe as always if you have any questions comments or feedback please leave them below click that like button likes are always appreciated please subscribe to get notified whenever a new video is uploaded thanks for watching i'll see you in next video
Info
Channel: TheTechPaper
Views: 2,540
Rating: undefined out of 5
Keywords: .Net Core, .Net Core MVC, ASP.Net, ASP.Net Core, 404 Errors in ASP.Net Core MVC, Visual Studio 2022, Handle 404 Errors in ASP.Net Core, Handle 404 Errors in ASP.Net Core MVC, Errors in .Net Core MVC, Errors in ASP.Net Core MVC, .Net Core API, .Net Core Web Api, Web Api, .Net 6, Error handling in .Net Core Web Api, Error handling in Web Api, Error handing in .Net core rest api
Id: Mk-KHFUyfL8
Channel Id: undefined
Length: 9min 48sec (588 seconds)
Published: Fri Jun 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.