Redirect to Action in another controller

拈花ヽ惹草 提交于 2019-12-17 15:12:12

问题


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

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?


回答1:


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

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

Or

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

depending on which area you're aiming for.




回答2:


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:

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

MSDN




回答3:


You can use this:

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



回答4:


Use this:

    return this.RedirectToAction<AccountController>(m => m.LogIn());


来源:https://stackoverflow.com/questions/10785245/redirect-to-action-in-another-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!