liveBook · Manning

RedirectToRoute

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

RedirectToRoute is a ControllerBase method that creates a redirection to a URL selected by the routing system from supplied route values.

RedirectToRoute is a ControllerBase method that asks the routing system to create a redirect URL from supplied route values, such as a controller, action, and identifier. section 19.5.3

Use it when the destination should be selected through routing rather than written as a literal URL:

  • Supply segment values in an anonymous object, including controller, action, and Id. section 19.5.3
  • Let convention routing select the matching route; this suits complex routing configurations but can redirect to the wrong URL if values are incorrect. section 19.5.3
  • Choose RedirectToRoutePermanent when the redirect should be permanent. section 19.5.3

Redirect to a product action

public IActionResult Redirect() { return RedirectToRoute(new { controller = "Products", action = "GetProduct", Id = 1 }); } This supplies route values for the Products controller’s GetProduct action and product identifier 1. section 19.5.3

...
[HttpGet("redirect")]
public IActionResult Redirect() {
   return RedirectToRoute(new {
       controller = "Products", action = "GetProduct", Id = 1
   });
}
...

View the complete controller snippet showing RedirectToRoute with controller, action, and Id route values. section 19.5.3

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

How deep it goes
Explains the method signature pattern and routing behavior, while warning against incorrect route selection without detailing routing internals.
Examples
Uses a ProductsController redirect targeting GetProduct with controller, action, and Id values.
What you can do after
Apply it via ASP.NET Core ControllerBase to generate redirects from route values.
How it connects
Connects ControllerBase, action methods, route values, convention routing, 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.