RedirectToAction 如何不命中我的 Action?

发布于 2024-10-12 00:33:26 字数 852 浏览 0 评论 0原文

我在 MVC 应用程序中发现了一些意外的行为。

假设我有 3 个操作方法

  • Details
  • Details_Fr
  • Details_En

第二个和第三个背后的想法是它们切换语言,然后重定向到“真正的”详细信息操作。

但是,当我使用“详细信息”中的断点调用 RedirectToAction 时,无法到达它。当我按“Controller/Details”顺序访问页面并从那里开始“Controller/Details_Fr”时,就是这种情况。

以下是我的操作:

    public ActionResult Details()
    {
        return View(new MyViewModel());
    }

    public ActionResult Details_Fr()
    {
        this.SetLanguage(CultureInfo.GetCultureInfo("fr-CA"));
        return RedirectToAction("Details");
    }

    public ActionResult Details_En()
    {
        this.SetLanguage(CultureInfo.GetCultureInfo("en-US"));
        return RedirectToAction("Details");
    }

我不是在寻找解决方案,因为通过将 RedirectToAction 更改为 View(new MyViewModel()) 可以轻松完成此操作。我正在寻找解释,以便我了解发生这种情况的原因和原因。

谢谢!

I've been seeing some unexpected behavour in my MVC application.

Lets say I have 3 action methods

  • Details
  • Details_Fr
  • Details_En

The idea behind the 2nd and 3rd is that they switch the language and then redirect to the "real" Details action.

However, when I call RedirectToAction with a breakpoint in "Details" it is not reached. This is the case when I visit the pages in this order "Controller/Details" and from there "Controller/Details_Fr".

Here are my actions:

    public ActionResult Details()
    {
        return View(new MyViewModel());
    }

    public ActionResult Details_Fr()
    {
        this.SetLanguage(CultureInfo.GetCultureInfo("fr-CA"));
        return RedirectToAction("Details");
    }

    public ActionResult Details_En()
    {
        this.SetLanguage(CultureInfo.GetCultureInfo("en-US"));
        return RedirectToAction("Details");
    }

I'm not looking for a solution as that's easily done by changing RedirectToAction to View(new MyViewModel()). I am looking for an explaination so I understand what and why this is happening.

Thanks!

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

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

发布评论

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

评论(1

夜吻♂芭芘 2024-10-19 00:33:26

您应该在 ActionFilter 属性中设置语言(CurrentCulture 和 CurrentUICulture on CurrentThread),而不是创建那些可怕的 _Fr 和 _En 操作......!

You should be setting the language (CurrentCulture and CurrentUICulture on CurrentThread) in ActionFilter attribute instead of creating those horrible _Fr and _En actions....!

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