title: Set ViewBag property in the constructor of a ASP.NET MVC Core controller
body: <p>My theme has some sort of breadcrumb. The controller is always the category. To avoid repeat myself, I want to set it in the constructor of the controller for all actions like this: </p>
  
  <pre><code>class MyController:Controller{
      public MyController() {
          ViewBag.BreadcrumbCategory = "MyCategory";
      }
  }
  </code></pre>
  
  <p>When I access <code>ViewBag.BreadcrumbCategory</code> in the layout-view, its null. In a Action it works:</p>
  
  <pre><code>class MyController:Controller{
      public IActionResult DoSomething() {
          ViewBag.BreadcrumbCategory = "MyCategory";
      }
  }
  </code></pre>
  
  <p>I'm wondering that setting a ViewBag property is not possible in a constructor? It would be annoying and no good practice to have a function called on every action which do this work. In <a href="https://stackoverflow.com/questions/9930797/declare-viewbag-on-controller">another question</a> using the constructor was an accepted answear, but as I said this doesn't work, at least for ASP.NET Core. </p>