How to reuse views (pages) in ASP.NET Core MVC?

2019-06-03 12:44发布

问题:

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?

回答1:

There is a Application Parts concept in ASP.NET Core:

An Application Part is an abstraction over the resources of an application, from which MVC features like controllers, view components, or tag helpers may be discovered.

Add the following into .csproj (for your library with views) to include views as embedded resources into the dll:

<ItemGroup>
   <EmbeddedResource Include="Views\**\*.cshtml" /> 
</ItemGroup>

then add assembly as app part and register the ViewComponentFeatureProvider for View discovery:

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}

Another way is to use the EmbeddedFileProvider. This approach is described in this SO answer.



回答2:

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:

        ...
        var assembly = typeof( Iramon.Web.Common.ViewComponents.ActiveAccountViewComponent).GetTypeInfo().Assembly;
        var part = new AssemblyPart(assembly);

        services.AddMvc()
            .ConfigureApplicationPartManager(p => {                    
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());                    
            });        

        var embeddedFileProvider = new EmbeddedFileProvider(
            assembly
        );

        //Add the file provider to the Razor view engine
        services.Configure<RazorViewEngineOptions>(options =>
        {                
            options.FileProviders.Add(embeddedFileProvider);
        });    


回答3:

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.