RedirectToAction 根本不起作用

发布于 2024-12-26 03:21:50 字数 273 浏览 1 评论 0原文

在 AccountController 中,在一个方法的末尾,我有:

RedirectToAction("EveryView", "Account");

在同一个控制器文件中,我有这个方法:

public ActionResult EveryView()
    {
        return View();
    }

但这个方法永远不会被调用。我在“{”上有一个断点,但它永远不会被击中!

In the AccountController, in the end of a method I have:

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

恰似旧人归 2025-01-02 03:21:50

你已经输入了“返回”,否则它不会重定向。

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

you have put 'return' else it won't redirect.

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

一抹苦笑 2025-01-02 03:21:50

嗯……很难看出(或说出)问题是什么,因为代码看起来相当简单。

也许一点调试可能会有所帮助!
尝试创建一个新的 TestController,在默认的 Index() ActionResult 中执行以下操作:

return RedirectToAction("EveryView", "Test");

然后,创建 EveryView() ActionResult 方法并设置断点。

public ActionResult EveryView()
{
    return View();
}

如果您尝试 http://localhost/Test/Index 会发生什么?有效吗?

如果这不起作用,也许您可​​能需要查看您的路由并确保没有可能导致问题的特殊路由定义。

或者,您可以在 Global.asax 中添加此方法:

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
}

并在该行上设置一个断点以捕获任何未知错误。

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:

return RedirectToAction("EveryView", "Test");

Then, create the EveryView() ActionResult method and set your break point.

public ActionResult EveryView()
{
    return View();
}

If you try http://localhost/Test/Index what happens? Does it work?

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.

Alternatively, you could, inside your Global.asax add this method:

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.

内心旳酸楚 2025-01-02 03:21:50

我在最近的项目中也遇到了这个问题。我最终通过在重定向操作上添加 [AllowAnonymous] 解决了这个问题,因为控制器用 [Authorize] 属性装饰,此时用户尚未登录。希望这对您有所帮助。

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.

夜深人未静 2025-01-02 03:21:50

如果 Action 方法没有被调用,可能是因为该方法已为 HttpPost 注册,但您正在发送 HttpGet 请求。因此,最好使用 [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] 保留接受两个请求的方法

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)]

农村范ル 2025-01-02 03:21:50

我遇到了这个问题,这是因为我添加的自定义 MustBeLoggedIn 过滤器不允许重定向到该方法,类似于 user2192287。

这意味着过滤器将我重定向回我开始的操作。

这可能是 petko_stankoski 的问题,因为它出现在他们的帐户控制器中。

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.

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.

花期渐远 2025-01-02 03:21:50

我有类似的问题。我刚刚从函数中删除了 post 属性 [HttpPost] 到我重定向的位置。

I had a similar problem. I've just removed the post attribute [HttpPost] from the function to where I've redirected.

美人迟暮 2025-01-02 03:21:50

你必须归还它

    public ActionResult Pay(string id)
    {
        return RedirectToAction("EveryView", "Account");;
    }

you have to return it

    public ActionResult Pay(string id)
    {
        return RedirectToAction("EveryView", "Account");;
    }
冷清清 2025-01-02 03:21:50

注释所有已加载的脚本并重试。某些脚本受到此对象的干扰

Comment all loaded Scripts and try again.some script Interference by this object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文