重定向到另一个控制器中的操作

时间:2022-12-01 22:44:33

I have two controllers, both called AccountController. One of them, lets call it Controller A, is in an Area called Admin and the other, lets call it Controller B, is not in any Area (I guess that means it's in the default Area?). Controller B has an action method called Login. I have an action method in Controller A, which has this line

我有两个控制器,都叫AccountController。其中一个,我们叫它控制器A,在一个叫做Admin的区域,另一个叫控制器B,它不在任何区域(我想这意味着它在默认区域?)控制器B有一个名为Login的操作方法。我在控制器A中有一个动作方法,它有这条线

return RedirectToAction("LogIn", "Account");

The problem is that I get a 404 when this line gets executed because an attempt is made to redirect to a non-existent action in Controller A. I want to call the action method in Controller B. Is this possible?

问题是,当执行这一行时,我得到404,因为试图重定向到控制器a中不存在的操作,我想调用控制器b中的操作方法,这可能吗?

3 个解决方案

#1


182  

You can supply the area in the routeValues parameter. Try this:

您可以在routeValues参数中提供区域。试试这个:

return RedirectToAction("LogIn", "Account", new { area = "Admin" });

Or

return RedirectToAction("LogIn", "Account", new { area = "" });

depending on which area you're aiming for.

这取决于你的目标区域。

#2


19  

Use this:

用这个:

return RedirectToAction("LogIn", "Account", new { area = "" });

This will redirect to the LogIn action in the Account controller in the "global" area.

这将重定向到“全局”区域的帐户控制器中的登录操作。

It's using this RedirectToAction overload:

它使用了RedirectToAction重载:

protected internal RedirectToRouteResult RedirectToAction(
    string actionName,
    string controllerName,
    Object routeValues
)

MSDN

MSDN

#3


2  

You can use this:

您可以使用:

return RedirectToAction("actionName", "controllerName", new { area = "Admin" });

#1


182  

You can supply the area in the routeValues parameter. Try this:

您可以在routeValues参数中提供区域。试试这个:

return RedirectToAction("LogIn", "Account", new { area = "Admin" });

Or

return RedirectToAction("LogIn", "Account", new { area = "" });

depending on which area you're aiming for.

这取决于你的目标区域。

#2


19  

Use this:

用这个:

return RedirectToAction("LogIn", "Account", new { area = "" });

This will redirect to the LogIn action in the Account controller in the "global" area.

这将重定向到“全局”区域的帐户控制器中的登录操作。

It's using this RedirectToAction overload:

它使用了RedirectToAction重载:

protected internal RedirectToRouteResult RedirectToAction(
    string actionName,
    string controllerName,
    Object routeValues
)

MSDN

MSDN

#3


2  

You can use this:

您可以使用:

return RedirectToAction("actionName", "controllerName", new { area = "Admin" });