asp.net-mvc 重定向到另一个控制器中的操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10785245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:59:11  来源:igfitidea点击:

Redirect to Action in another controller

asp.net-mvcasp.net-mvc-3c#-4.0redirecttoaction

提问by Sachin Kainth

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

我有两个控制器,都称为AccountController. 其中一个,让我们调用它Controller A,在一个Area被调用中Admin,另一个,让我们调用它Controller B,不在任何一个Area(我猜这意味着它在默认值中Area?)。 Controller B有一个action methodLogin. 我有一个action methodin Controller A,里面有这条线

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

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

问题是404当这条线被执行时,我得到了一个,因为试图重定向到一个不存在actionController A. 我想调用action methodin Controller B。这可能吗?

回答by Rory McCrossan

You can supply the areain the routeValuesparameter. Try this:

您可以arearouteValues参数中提供 。尝试这个:

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

Or

或者

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

depending on which area you're aiming for.

取决于您的目标区域。

回答by gdoron is supporting Monica

Use this:

用这个:

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

This will redirect to the LogInaction in the Accountcontroller in the "global" area.

这将重定向到“全局”区域中控制器中的LogIn操作Account

It's using this RedirectToActionoverload:

它正在使用这个RedirectToAction重载:

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

MSDN

MSDN

回答by Muhammad Awais

You can use this:

你可以使用这个:

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

回答by Hiren Patel

Use this:

用这个:

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