Before ASP.NET Core MVC, we would use RazorGenerator to compile views into assemblies, and we would reuse those views in another project, by introducing a custom ViewEngine that would load the view from the assembly instead of file-system.
In ASP.NET Core MVC there is this concept of pre-compiled views and it works out of the box for version 2.0 and creates an assembly that by convention has the name of project_name.PrecompiledViews.dll.
I have two problems though that I can't find an answer for on Google.
First I don't know how to reuse that DLL in another project. Like if I have About.cshtml
page in CompanyBase.dll
, how can I reuse that page/view in ProjectAlpha
?
And also I don't want view compilation to happen on publish. How can I change it to happen on build?
There is a Application Parts concept in ASP.NET Core:
Add the following into .csproj (for your library with views) to include views as embedded resources into the dll:
then add assembly as app part and register the
ViewComponentFeatureProvider
for View discovery:Another way is to use the
EmbeddedFileProvider
. This approach is described in this SO answer.I've had success using views compiled into a dll using the following framework:
https://github.com/ExtCore/ExtCore
The whole framework may not be of use to you but you could certainly analyse the source code to see how that framework achieves it.
If you want to use views from other assemblies EmbeddedFileProvider has to be used in addition to ViewComponentFeatureProvider.
So the complete ConfigureServices should be something like: