liveBook · Manning

Http redirect

As explained in ASP.NET Core in Action, 3e

An HTTP redirect is a response that instructs a client to issue a request for a different URL.

An HTTP redirect tells the browser to request a different URL. In a Razor Pages application, it moves users between pages without requiring them to change the address manually. section 15.4.1

Use redirects when:

  • A user submits a form and should continue to another page, such as moving from checkout to payment. section 15.4.1
  • An application converts an error response into a request for a friendly error page. section 15.5
  • The browser should make a second request to the destination URL; the original response and follow-up request remain separate. section 15.5

Redirecting error pages

Configure status-code middleware with app.UseStatusCodePagesWithRedirects("/{0}");. A 404 response becomes a 302 redirect, and the browser requests /404 to generate the error page with a 200 response. section 15.5

Figure 15.3 A typical POST, REDIRECT, GET flow through a website. A user sends their shopping basket to a checkout page, which validates its contents and redirects to a payment page without the user’s having to change the URL manually.
CH15_F03_Lock3

A POST, REDIRECT, GET diagram makes the checkout flow concrete: the browser follows the redirect and reaches the payment page automatically. section 15.4.1

Redirects differ from returning a page directly. A PageResult returns HTML for the current request, whereas a RedirectToPageResult sends the browser to another page. For error handling, re-execution preserves the original status code; redirection loses it, so refreshing requests the error URL instead. section 15.4.1 section 15.5

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains browser follow-up requests, 302 and 200 responses, and status-code loss while stopping short of redirect variants.
Examples
Uses checkout-to-payment navigation and status-code middleware redirecting a 404 response to /404.
What you can do after
Operate/configure Razor Pages navigation and error handling with redirect-based results and status-code middleware.
How it connects
Connects HTTP status codes, Razor Pages, page handlers, middleware, and POST-redirect-GET navigation.

Also explained in

Compare how 2 books explain http redirect →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.