i18n与RenderAction [英] i18n with RenderAction
问题描述
我在视图中有一些片段,如下所示:
I have some fragments in a view as follows:
@Html.RenderAction("Foo","Home")
然后所有控制器都扩展BaseController
Then all the controllers extends BaseController
class HomeController : BaseController{
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
.... // http://afana.me/post/aspnet-mvc-internationalization.aspx
}
}
发生的是,对于每个RenderAction调用,也会执行一次.但这并不需要导致它们是子行为.如果我有3次RenderAction调用,则上面的代码将被执行3次,这是不应该的.
What happens is, for every RenderAction call, this is executed as well. But it doesnt need to cause they are child actions. If i have 3 RenderAction call, the above code is being executed 3 times, which it shouldn't.
我只需要每个请求执行一次.
I need this executed only once per request.
我该如何正确完成?还是应该将此代码放在其他地方?
How can i get this done properly? Or should i put this code somewhere else?
推荐答案
这是ASP.NET MVC设计的.这是一个模板方法,它将始终被调用.
This is by design from ASP.NET MVC. This is a template method, and it will always be called.
但是,如果出于性能原因要跳过代码执行(如果该调用是针对子操作的话).用以下if语句包装代码:
However, if you want to skip code execution for performance reasons if the call is for a child action. Wrap the code with an if statement like this:
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
if (!ControllerContext.IsChildAction) {
.....
}
}
这篇关于i18n与RenderAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!