liveBook · Manning

RedirectToAction

As explained in Pro ASP.NET Core 7, Tenth Edition

RedirectToAction is a ControllerBase method that creates a redirection to a URL generated for a specified action method.

RedirectToAction is a ControllerBase method that returns a temporary redirection to a URL generated for another action method. Routing creates the destination instead of requiring a manually written URL. section 19.5.3

Use it when an action should redirect within the controller or to another controller:

  • Select the target with an action-name string or nameof, which avoids action-name typos. section 19.5.3
  • Pass route values through an anonymous object, such as new { Id = 1 }. section 19.5.3
  • Omit the controller name to target the current controller; use the overload accepting action and controller names for another controller. section 19.5.3
  • Choose RedirectToActionPermanent when the redirect should be permanent rather than temporary. section 19.5.3

Redirect to a product action

[HttpGet("redirect")] public IActionResult Redirect() { return RedirectToAction(nameof(GetProduct), new { Id = 1 }); } Routing generates a URL for GetProduct, and the client follows the temporary redirect to the product endpoint. section 19.5.3

Listing 19.23 Redirecting in the ProductsController.cs file in the Controllers folder
...
[HttpGet("redirect")]
public IActionResult Redirect() {
    return RedirectToAction(nameof(GetProduct), new { Id = 1 });
}
...

Inspect the complete controller listing showing RedirectToAction with nameof and an anonymous route-value object. section 19.5.3

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains overloads, nameof, route values, and temporary versus permanent redirects, but does not detail redirect response internals.
Examples
Demonstrates ProductsController redirecting to GetProduct with Id = 1 and testing the resulting product response.
What it assumes
Assumes familiarity with controller actions and routing while showing the redirect implementation from a ControllerBase action.
What you can do after
Build it from scratch by returning RedirectToAction with an action name and optional route values.
How it connects
Connects controller actions, ControllerBase, routing, route values, and permanent redirects.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.