asp.net-mvc 如何重定向到同一控制器中的另一个动作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10353931/
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:54:29  来源:igfitidea点击:

how to redirect to another action in same controller?

asp.net-mvcasp.net-mvc-3redirectcontrolleraction

提问by dinesh

i want to redirect to another action in same controller. how can we achieve this? i tried like return

我想重定向到同一个控制器中的另一个动作。我们怎样才能做到这一点?我试过喜欢返回

RedirectToAction("NotAuthorized");

RedirectToAction("NotAuthorized");

回答by Leniel Maccaferri

return RedirectToAction("ActionName");

Instead of return Redirect("/Elearning/NotAuthorized");do this:

而不是return Redirect("/Elearning/NotAuthorized");这样做:

return RedirectToAction("NotAuthorized"); // if you're inside the Elearning controller

or

或者

RedirectToAction("NotAuthorized", "Elearning"); // if calling from other controller

回答by itsmatt

Try

尝试

return RedirectToAction("SomeAction");

回答by Brandon

If you want to return a redirect

如果你想返回一个重定向

return RedirectToAction("NotAuthorized");

is the valid way to do this. Make sure that your method actually exists.

是做到这一点的有效方法。确保您的方法确实存在。

Alternatively, if you don't want a redirect

或者,如果您不想重定向

return View("NotAuthorized");

works as well.

也有效。

回答by Rosario Capuno

try to explore the ControllerBase first, or go to the definition of the Controller which is what your controller inherits and then look for 'redirect'

尝试首先探索 ControllerBase,或者转到控制器的定义,这是您的控制器继承的,然后查找“重定向”

回答by user864675

I am using asp.net MVC 3 and this works: return Redirect("/{VIEWPATH}/{ACTIONNAME}");.

我正在使用 asp.net MVC 3,这有效:return Redirect("/{VIEWPATH}/{ACTIONNAME}");.

Example, return Redirect("/account/NotAuthorized");where 'account' is your view path and your controller name is AccountController. Hope this helps.

例如,return Redirect("/account/NotAuthorized");其中 'account' 是您的视图路径,您的控制器名称是 AccountController。希望这可以帮助。