Rendering Views from external assemblies in MVC6
To load views from a separate assembly, you need
CompositeFileProvider. I provide more information on how to use these here: https://stackoverflow.com/a/34366119/188740
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
CompositeFileProvider, both of which are new, so you'll need to get them from the
Reference them in your startup MVC6 project's
Update your service registration in
options.FileProvider = new CompositeFileProvider(
"BookStore.Portal" // your external assembly's base namespace
project.json of your external assembly, add this:
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:
options.ViewLocationExpanders.Add( new MyViewLocationExpander());
If you need load view from "outside" of app, check this: https://www.mikesdotnetting.com/article/301/loading-asp-net-core-mvc-views-from-a-database-or-other-location
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.
It seems that adding the base namespace doesn't work as expected. Don't really know why
How to Get Number of Rows Using Sqldatareader in C#
How to Create a Dynamic Email Template That Can Be Modified Without Changing Code in C# .Net MVC
How to Check Byte Array Empty or Not
How to Generate a System (Pc/Laptop) Hardware Unique Id in C#
Restsharp Serializing Json Objects to Post Parameters
Asp.Net Core Form Post Results in a Http 415 Unsupported Media Type Response
Load Image from Resources Area of Project in C#
Read and Parse a Json File in C#
How to Check If a Socket Is Connected/Disconnected in C#
How to Get the Current Project Directory from C# Code When Creating a Custom Msbuild Task
Validate Indian Phone Number With Optional +91 or 0 Preceeding 10 Digits
How to Convert Json Date to .Net Datetime During Deserialization
Reload Page After Jquery.Get to MVC Controller Action
What Is the Purpose of a Question Mark After a Type (For Example: Int Myvariable)
How Does Httpcontext.Current.User.Identity.Name Know Which Usernames Exist
Posting Jsonobject With Httpclient from Web API