📜  RedirectToAction net core 3.1 with area (1)

📅  最后修改于: 2023-12-03 15:19:47.313000             🧑  作者: Mango

RedirectToAction in .NET Core 3.1 with Areas

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.

Syntax
public virtual RedirectToActionResult RedirectToAction(
    string actionName,
    string controllerName,
    object routeValues
);
Parameters

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.

Examples
Redirect to another action within the same Controller

To redirect to an action within the same Controller, use the following code:

return RedirectToAction("Index");
Redirect to an action in another Controller

To redirect to an action in another Controller, use the following code:

return RedirectToAction("Index", "Home");
Redirect to an action in another Area

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.

Conclusion

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.