Part 72. UseStatusCodePagesWithRedirects Vs UseStatusCodePagesWithReExecute in ASP NET Core MVC.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] foreign [Music] Concepts my name is Amit and this is part 72 of the asp.net core MVC tutorial in this video we will learn the difference between use status code pages with redirects and use status code pages with re-execute this is a continuation part 71 handle 404 errors in asp.net core NPC so please watch it before proceeding to this one I'll share the link in the description first let's learn about use status code pages with redirects the use status code pages with redirects extension method sends a 302 found status code to the client this is really important it always sends a 302 error code irrespective of the error generated whether it is a 404 page not found exception or anything else next it redirects the client to the error handling endpoint provided in the URL template the error handling endpoint typically displays error information and returns HTTP 200 like any custom error page all right enough Theory let's go to the visual studio and this is the project which we are working on let's use use status code pages with redirects and then run your application press F12 navigate to network tab navigate to any URL which is not available in our application let's say slash ameth let's go to the developer tab If You observe carefully the first request Amit Mouse over and you can see the complete URL which we try to access has a status code 302 this is just what we specified in our slide sensor 302 found status code to the client all right the next point in our slide is to redirect the client to the error handling endpoint provided in the URL template if you look at the program.cs class we specify status code error as an endpoint or you can say it as an URL within its location or URL template so if you look at the URL of our application in browser we try to access slash ameth but this middleware use status code pages with redirects redirects us to status code error URL let's go to slide again another important point is the error handling and points typically display error information and return http 200. to display such errors we created our custom view which is home controller and error action method so we covered that and it returns HTTP 2 Double Zero status code let's confirm that let's go to the developer window Mouse over 404 you can see this is the page which the end user gets after it is redirected and its status code is 200 200 status code means success or okay simple extremely simple all right let's see when should we use this method this method is commonly used when the app should redirect the client to a different endpoint usually in cases where a different app processes the error for web apps the client's browser address bar reflects the redirected endpoint we can also use this when we shouldn't preserve and return the original status code with the initial redirect response 302 found redirect status Response Code indicates that the resource requested has been temporarily moved to the URL given by the location header so the user enters the correct URL but because of any reason its URL is changed to something else then also you can use this method all right next use status code pages with re-execute so the use status code Pages 3 execute extension method generates the response Body by re-executing the request pipeline using an alternate path does not alter the status code before or after re-executing the pipeline unlike use status code pages with redirects where the status code changes to 302 this middleware extension method don't modify the status code all right another important thing to remember is that although this middleware doesn't modify the status code the new pipeline or you can see the next middleware in the pipeline May alter the response status code as the new pipeline has full control of the status code if the new pipeline does not alter the status quo the original status code will be sent to the client the URL template must start with Slash and may include placeholder zero within curly places for status code to pass the status code as a query string parameter pass a second argument into use status code pages with re-execute all right let's get back to visual studio change Hue status code pages with redirects to use status code pages with re-execute and run your application press F12 navigate to network tab navigate to any URL which is not available in our application let's say slasher meth and if You observe the URL s tell the same it does not redirect to status code error URL now go to developer tab the page omit which we try to access has 404 error so there is no modification to the original status code next let's see the query string option which we discussed in the slide let's go to the visual studio and stop the application so what do we have to do to pass the status code as a query string parameter pass a second argument into use status code pages with re-execute so instead of placeholder remove the Slash and type question mark status code is equal to 0 within curly braces let's go to home controller now within the route we will get the status code so remove it from the route and that's it let's put a breakpoint and run our application navigate to slash ameth and you can see the debugger hit our action method and we get the required status code as a query string similarly if you want to pass some other information as a query string you can create it in the middleware within the program.cs class remove the breakpoint and press F5 as you can see we have the required response all right let's see when should we use this extension method let's go to slide this method is commonly used when the app should process the request without redirecting to the different endpoint for web apps the client's browser address bar reflects the originally requested endpoint preserve and return the original status code with the response all right another use case specified in the Microsoft site is the endpoint should process the error can get the original URL that generated the error this is important because in the live project we not only display some error pages but we also capture logs if we only get the status code but not the info from where the error is generated or what kind of URL user tries to access then it is a problem for developer let's see how to implement that let's go to the visual studio let's say where status code re-execute feature is equal to HTTP context dot features dot get then I status code re-execute feature let's check if we get the required object or not so if status code re-execute feature is not null Curry braces viewback dot original path is equal to status code re-execute feature Dot it has different options like original path base original query string and let's use original path which will return the path which end user tries to execute let's display the same in our view so let's go to index View back dot original path is equal to at view back dot original path and that's it let's run our application navigate to slash Amit as you can see we get the required path that we try to browse simple extremely simple 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: 287
Rating: undefined out of 5
Keywords: app.usestatuscodepageswithreexecute not working, mvc core usestatuscodepageswithreexecute, usestatuscodepageswithreexecute example, usestatuscodepageswithreexecute get url, usestatuscodepageswithreexecute not working, usestatuscodepageswithredirects not working, usestatuscodepageswithredirects .net core, usestatuscodepageswithreexecute vs usestatuscodepageswithredirects, asp.net core, asp.net core tutorial, asp net mvc tutorial, dotnet core, asp.net mvc core tutorial, ProConcepts
Id: oct9D_b9rvQ
Channel Id: undefined
Length: 10min 32sec (632 seconds)
Published: Sat Mar 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.