📅  最后修改于: 2023-12-03 15:19:47.313000             🧑  作者: Mango
In ASP.NET Core 3.1, RedirectToAction
method can be used in a Controller action to redirect the user to another action within the same Controller or to another Controller. This method can be especially useful when working with Areas as it can navigate to an action within the same Area or to an action in another Area.
public virtual RedirectToActionResult RedirectToAction(
string actionName,
string controllerName,
object routeValues
);
The actionName
parameter is the name of the action method within the Controller to which the redirect will happen.
The controllerName
parameter is the name of the Controller to which the redirect will happen.
The routeValues
parameter is an optional anonymous object that contains additional parameters to pass to the action method.
To redirect to an action within the same Controller, use the following code:
return RedirectToAction("Index");
To redirect to an action in another Controller, use the following code:
return RedirectToAction("Index", "Home");
To redirect to an action in another Area, use the following code:
return RedirectToAction("Index", "Home", new { area = "Admin" });
In this example, the area
parameter is set to "Admin"
which indicates that the action that is being redirected to is located in the Admin Area.
RedirectToAction
is a useful method that can help to easily redirect users between actions within Controllers and Areas. By understanding how to use this method, developers can create more intuitive and seamless web applications.