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
