title: ASP.NET MVC 6: view components in a separate assembly
body: <p>I'd like to define view components (which are new in ASP.NET MVC 6) in a separate assembly from the MVC 6 web startup project so that I can reuse them in multiple web projects.  A sample solution might look like this:</p>
  
  <ul>
  <li>BookStore.Components <em>(houses common view components)</em></li>
  <li>BookStore.Web1 <em>(references BookStore.Components)</em></li>
  <li>BookStore.Web2 <em>(references BookStore.Components)</em></li>
  </ul>
  
  <p>I created a new Class Library (Package) and created a view component inside.  I also created the view following the nested folder convention.  My <code>BookStore.Components</code> project looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/3RkQ2.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/3RkQ2.jpg" alt="enter image description here"></a></p>
  
  <p>When I try to invoke this view component from my web project:</p>
  
  <pre><code>@Component.Invoke("BookOfTheMonth")
  </code></pre>
  
  <p>...I get a 500 error with an empty content body.  It seems like the ViewComponent class is discovered, but the razor view for the component isn't.</p>
  
  <p>I also tried to extend <code>DefaultViewComponentDescriptorProvider</code> so that view components from the <code>BookStore.Components</code> assembly can be discovered:</p>
  
  <h3>Defined an AssemblyProvider</h3>
  
  <pre><code> public class AssemblyProvider : IAssemblyProvider
      {
          public IEnumerable&lt;Assembly&gt; CandidateAssemblies
          {
              get
              {
                  yield return typeof(AssemblyProvider).Assembly;
                  yield return typeof(BookStore.Components.BookOfTheMonthViewComponent).Assembly;
              }
          }
      }
  </code></pre>
  
  <h3>Registered AssemblyProvider using Autofac</h3>
  
  <pre><code>builder.RegisterType&lt;AssemblyProvider&gt;()
      .AsImplementedInterfaces();
  
  builder.RegisterType&lt;DefaultViewComponentDescriptorProvider&gt;()
      .AsImplementedInterfaces();
  </code></pre>
  
  <p>I'm not sure if the registration of <code>DefaultViewComponentDescriptorProvider</code> above is needed or not, so I tried with and without it, but I still get a 500 error on a page where the view component is invoked.</p>
  
  <p>How can I invoke a view component that lives in a separate assembly from the MVC6 web project?</p>