I am converting a legacy ASP.Net MVC website to Core, and I used to have all controllers inherit from a BaseController class which was overriding Initialize
and setting some properties on the ViewBag
. There seems to be no Initialize
method now, so I'm doing that through the constructor. However, by the time I get to the Action method, it seems the ViewBag
has been reset.
The code is basically like this:
public abstract class BaseController : Controller
{
protected BaseController()
{
ViewBag.Hello = "hello";
}
}
public class MyController : BaseController
{
public IActionResult Index()
{
var hello = ViewBag.Hello;
// but hello is null
return View();
}
}
Should I be overriding a different method rather than going through the constructor? Where is ViewBag
getting reset and what is a better way to work around that?