Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a base controller class:

And all my other controller inherits this BaseClass like this

All this works great in MVC3 (test again today, it really works) but it seems that the ExecuteCore in BaseController is not fired any more in MVC 4 beta.

Any idea? Or Anything huge has changed under the hood? Thanks very much.

public class BaseController : Controller
{
    private string _myData;

    public string MyData
    {
        get
        {
            return _myData;
        }
    }

    protected override void ExecuteCore()
    {
        _myData = "I am doing something";

        base.ExecuteCore();
    }
}


public class HomeController : BaseController
{
    public ActionResult Index()
    {
        ViewBag.MyData = MyData;
        // Doing something with value in BaseClass

        return View();
    }
}
like image 240
BladeLeaf Avatar asked Mar 04 '12 13:03

BladeLeaf


3 Answers

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.

like image 107
nemesv Avatar answered Nov 02 '22 20:11

nemesv


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.

like image 28
Alfonso Muñoz Avatar answered Nov 02 '22 19:11

Alfonso Muñoz


You can also use BeginExecuteCore

protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
    return base.BeginExecuteCore(callback, state);
}
like image 7
LungFungus Avatar answered Nov 02 '22 20:11

LungFungus