RedirectToAction 如何不命中我的 Action?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在 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....!