asp.net-mvc RedirectToAction 根本不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8806074/
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
RedirectToAction not working at all
提问by petko_stankoski
In the AccountController, in the end of a method I have:
在 AccountController 中,在一个方法的最后,我有:
RedirectToAction("EveryView", "Account");
In the same controller file I have this method:
在同一个控制器文件中,我有这个方法:
public ActionResult EveryView()
{
return View();
}
But this method never gets called. I have a breakpoint on '{' and it never gets hit!
但是这个方法永远不会被调用。我在 '{' 上有一个断点,它永远不会被击中!
采纳答案by Vlince
Humm…difficult to see (or say) what the problem is since the code seems pretty trivial.
嗯……很难看出(或说出)问题是什么,因为代码看起来很简单。
Perhaps a little debugging might help! Try creating a new TestController have inside the default Index() ActionResult do this:
也许一些调试可能会有所帮助!尝试在默认的 Index() ActionResult 中创建一个新的 TestController 执行以下操作:
return RedirectToAction("EveryView", "Test");
Then, create the EveryView() ActionResult method and set your break point.
然后,创建 EveryView() ActionResult 方法并设置断点。
public ActionResult EveryView()
{
return View();
}
If you try http://localhost/Test/Indexwhat happens? Does it work?
如果你尝试http://localhost/Test/Index会发生什么?它有效吗?
If that doesn't work, perhaps you may want to look at your Routes and make sure you have no special Routes define that could make things break.
如果这不起作用,也许您可能想要查看您的 Routes 并确保您没有特殊的 Routes 定义可能会使事情中断。
Alternatively, you could, inside your Global.asaxadd this method:
或者,您可以在您的内部Global.asax添加此方法:
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
}
And set a break point on the line to catch any unknown errors.
并在行上设置一个断点以捕获任何未知错误。
回答by Bonanza
you have put 'return' else it won't redirect.
你已经把'返回'否则它不会重定向。
return RedirectToAction("EveryView", "Account");
return RedirectToAction("EveryView", "Account");
回答by user2192287
I had this problem too in my recent project. I resolved it finally by adding [AllowAnonymous] on the redirected action, because the controller decorated with [Authorize] attribute, and at this point the user is not logged in. Hope this is a help.
我在最近的项目中也遇到了这个问题。我最终通过在重定向操作上添加 [AllowAnonymous] 来解决它,因为控制器装饰有 [Authorize] 属性,此时用户尚未登录。希望这是一个帮助。
回答by tekiegirl
I had this problem and it was because a custom MustBeLoggedIn filter I had added was not allowing the redirect to that method, similar to user2192287.
我遇到了这个问题,这是因为我添加的自定义 MustBeLoggedIn 过滤器不允许重定向到该方法,类似于 user2192287。
This meant that I the filter was redirecting me back to the action I was starting from.
这意味着我过滤器将我重定向回我开始的操作。
This may have been petko_stankoski's problem as it was in their Account controller.
这可能是 petko_stankoski 的问题,因为它在他们的帐户控制器中。
回答by ilans
I had a similar problem. I've just removed the post attribute [HttpPost] from the function to where I've redirected.
我有一个类似的问题。我刚刚从函数中删除了 post 属性 [HttpPost] 到我重定向的位置。
回答by Raj
If the Action method doesn't gets invoked, it could be because the method is registered for HttpPost but u are sending HttpGet request. So, better keep the method to accept both request using [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
如果没有调用 Action 方法,可能是因为该方法已为 HttpPost 注册,但您正在发送 HttpGet 请求。因此,最好保留使用 [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] 接受两个请求的方法
回答by MOHAMMAD
Comment all loaded Scripts and try again.some script Interference by this object
注释所有加载的脚本并重试。一些脚本被此对象干扰

