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();
}
}
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 totrue
if they still needExecuteCore
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.
You can also use BeginExecuteCore
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
return base.BeginExecuteCore(callback, state);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With