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

BladeLeaf picture BladeLeaf · Mar 4, 2012 · Viewed 9.7k times · Source

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();
    }
}

Answer

nemesv picture nemesv · Mar 4, 2012

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.