There is a GitHub problem about this, and he stated that it is by design. The answer you are linking up is ASP.NET MVC3, the old old ASP.NET stack.
ASP.NET Core is written from scratch and uses a variety of concepts designed both for portability (on multiple platforms) and for performance and modern practices such as built-in support for für Dependency Injection.
The latter does not allow you to set the ViewBag
in the constructor, because some properties of the Constructor
base class must be entered through the Injection Property, as you may have noticed that you do not need to pass these dependencies to your derived controllers.
This means that when the Controller
constructor is called, the properties for HttpContext
, ControllerContext
, etc. not installed. They are installed only after , called by the constructor, and there is a valid instance / reference to this object.
And as stated in the GitHub issues, it will not be fixed because it is by design.
As you can see here , the ViewBag is dependent on the ViewData
and the ViewData
populated after the controller is initialized. If you call ViewBag.Something = "something"
, then you will create a new instance of the DynamicViewData
class, which will be replaced with the one after the constructor initialization.
As @SLaks pointed out, you can use the action filter that you configure for each controller.
The following example assumes that you always derive your controllers from the base class Controller
.
public class BreadCrumbAttribute : IActionFilter { private readonly string _name; public BreadCrumbAttribute(string name) { _name = name; } public void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); var controller = context.Controller as Controller; if (controller != null) { controller.ViewBag.BreadcrumbCategory = _name; } } }
Now you can decorate your controller with it.
[BreadCrumb("MyCategory")] class MyController:Controller { }