Rendering Views from External Assemblies in Mvc6

Rendering Views from external assemblies in MVC6

To load views from a separate assembly, you need EmbeddedFileProvider and CompositeFileProvider. I provide more information on how to use these here:

Rendering Views from external assemblies in .NET 6 web app

So at least for .NET 6 you don't have to do anything special. Just reference your Razor Class Library and you have access to it. Nothing in the Startup (now Program). I did have to structure the project in the typical 'Controllers'/'Views' folder structure, but my calling application finds the controllers and views.

How to include controllers and views from an external project into MVC6?

Controllers will get loaded automatically. To load views, you will need EmbeddedFileProvider and CompositeFileProvider, both of which are new, so you'll need to get them from the aspnetvnext feed.

Reference them in your startup MVC6 project's project.json:

"Microsoft.AspNet.FileProviders.Composite": "1.0.0-*",
"Microsoft.AspNet.FileProviders.Embedded": "1.0.0-*",

Update your service registration in Startup.cs:

    services.Configure<RazorViewEngineOptions>(options =>
options.FileProvider = new CompositeFileProvider(
new EmbeddedFileProvider(
"BookStore.Portal" // your external assembly's base namespace

In project.json of your external assembly, add this:

  "resource": "Views/**"

Here's a sample implementation that you can clone and run to see it in action:

How to have razor find views from another assembly in a template service

Check this please
This is working only for searching views in app directory:

public class MyViewLocationExpander : IViewLocationExpander

public void PopulateValues(ViewLocationExpanderContext context)


public virtual IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
return viewLocations.Select(f => f.Replace("/Views/", "/Areas/Common/Views/"));


And in startup class:

services.Configure<RazorViewEngineOptions>(options =>
options.ViewLocationExpanders.Add( new MyViewLocationExpander());

If you need load view from "outside" of app, check this:

Is it possible to access MVC Views located in another project?

MVC does not compile views into DLL's, but instead references them as files from the root of your site directory. The location, by convention is ~/Views and a search path is followed. This is more or less hard coded into the default view engines.

Because Views are files, when you break them into a separate project, they won't exist in your primary web application project. Thus, the view engine can't find them. When you compile the app, any projects referenced will only copy the DLL's (and potentially a few other things, like pdb's, etc.)

Now, there are ways to work around this, but to be honest, they're usually more trouble than they're worth. You can look into "Portable Areas" in the mvc contrib project, but these are not well supported and there's been talk of replacing them with NuGet packaging.

You can also follow @mo.esmp's advice, and create a custom view engine, but you'll still need to figure out ways to copy the Views somewhere the site can access them upon build and/or deploy.

My suggestion would be to NOT break out projects in the manner you describe. I don't see any value in it. If your project becomes so large, I would instead separate your code into areas, and keep all your area code and data together.

What value is there in separating items that are clearly dependent upon each other into separate assemblies who's only purpose is to collect things based on their purpose? I see some value in separating models into their own project, since models can be used by more than one assembly. Controllers and views, however, are only ever used by the MVC primary site.

ASP.NET Core - Razor not checking for view embeded in assembly

I managed to get it working by removing the base namespace.

o.FileProviders.Add(new EmbeddedFileProvider(typeof(TestViewComponent)

It seems that adding the base namespace doesn't work as expected. Don't really know why

Related Topics

Leave a reply