ExecuteCore() in base class not fired in MVC 4 beta

痴心易碎 提交于 2019-12-02 17:09:05

I was able to reproduce your problem. It seems that the usage of ExecuteCore is changed. But I haven't find any information about it. My guess it's related to the fact that now the Controller implements IAsyncController not the AsyncController.

However I've found a workaround to get the old behavior with MVC4:

Add this to the BaseContoller :

protected override bool DisableAsyncSupport
{
    get { return true; }
}

From The MSDN page for DisableAsyncSupport (emphasis add by me):

This flag is for backwards compatibility. ASP.NET MVC 4. allows a controller to support asynchronous patterns. This means ExecuteCore doesn't get called on derived classes. Derived classes can override this flag and set to true if they still need ExecuteCore to be called.

I voted nemesv answer because it gave me an explanation about what is going on. I have MVC3 and MVC4 projects and this this was driving me mad.

However I have another solution. Override Initialize method in the Controller class:

public abstract class BaseController : Controller
{
  protected override void Initialize(System.Web.Routing.RequestContext requestContext)
  {
     string languageId = "en";
     try{
       // all your code here. You have access to all the context information, 
       // like querystring values:
       string languageId = requestContext.HttpContext.Request.QueryString["lang"];
       Thread.CurrentThread.CurrentUICulture = 
          CultureInfo.CreateSpecificCulture(languageId);
     }
     finally
     {
       Thread.CurrentThread.CurrentUICulture = 
         CultureInfo.CreateSpecificCulture(languageId);
     }

     base.Initialize(requestContext);
  }
}

Then in your project just make your controllers inherit from BaseController and that is all, BaseController call works automatically passing the request context. It works for both MVC3 and MVC4.

LungFungus

You can also use BeginExecuteCore

protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
    return base.BeginExecuteCore(callback, state);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!