pk_code
pk_code

Reputation: 2766

Difference between UseStatusCodePagesWithRedirects and UseStatusCodePagesWithReExecute - Status Code Pages in Asp.net core

I am using UseStatusCodePages Middleware to show status code pages on my application but it shows plain text on UI without any other information,

I want to show UI with Status Code Information along with some other helpful information like Customer Support Number with more user-friendly page.

I found out we can use two extension methods to do that which is UseStatusCodePagesWithRedirects and UseStatusCodePagesWithReExecute. Only Difference I found out from Microsoft Docs is,

UseStatusCodePagesWithRedirects : Send 302 to Client.

UseStatusCodePagesWithReExecute : Send Original Status Code and Executes handler for redirect URL.

Is that the only difference?

Upvotes: 5

Views: 4865

Answers (2)

TheRookieCoder
TheRookieCoder

Reputation: 31

When Using app.UseStatusCodePagesWithRedirects("/Error/{0}") and invalid request(lets say "/abc/xyz") is raised then;

  • Status Code 404 is issued, app.UseStatusCodePagesWithRedirects("/Error/{0}") intercepts the request and 302 status code is issued(which means URI of the requested resource has been changed temporarily)
  • As 302 is issued another get request is issued which results in change of the url from "/abc/xyz" to "/Error/404".
  • As the request is redirected to the specific error controller the status code for the request is 200 ok in the browser developer tool.

But When Using app.UseStatusCodePagesWithReExecute("/Error/{0}") and invalid request(lets say "/abc/xyz") is raised then;

  • app.UseStatusCodePagesWithReExecute("/Error/{0}") middleware intercepts the 404 status code and re-executes the pipeline pointing it to the URL

  • As the middleware is re executing the pipeline the original URL "/abc/xyz" in the address bar is preserved. It does not change from "/abc/xyz" to "/Error/{0}".

  • Also the original status Code(404 in this case) is preserved in the developer tool.

Upvotes: 3

DespeiL
DespeiL

Reputation: 1033

I think that the main difference is that UseStatusCodePagesWithRedirects is redirecting you to error controller action method while UseStatusCodePagesWithReExecute is just rendering page with out redirecting

Example

Controller actions

[Route("error/404")]
public IActionResult Error404(int code)
{

    return View("Error404");
}
[Route("error/{code}")]
public IActionResult Error(int code)
{
    return StatusCode(code);
}

Startup Cinfigue

 app.UseStatusCodePagesWithRedirects("/error/{0}");

or

  app.UseStatusCodePagesWithReExecute("/error/{0}");

Case 1 (404 Error)

Url : https://localhost:5001/notexits_page

1) UseStatusCodePagesWithRedirects
Result:

Url is: https://localhost:5001/error/404

We see Error404 page

2) UseStatusCodePagesWithReExecute

Result:

Url is: https://localhost:5001/notexits_page

We see Error404 page

Case2 (401 Error)

Url : https://localhost:5001/admin/users

1) UseStatusCodePagesWithRedirects
Result:

Url is: https://localhost:5001/error/401

We stack in infinity loop

1) UseStatusCodePagesWithRedirects
Result:

Url is: https://localhost:5001/admin/users

We see default browser error page for 401 error

Upvotes: 4

Related Questions