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

3

1

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?

Mollee answered 9/12, 2017 at 9:1 Comment(0)
D
4

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.

Diaconate answered 9/12, 2017 at 9:36 Comment(3)
Dear @Set, I'm getting this error and I couldn't solve this issue for two weeks now: Cannot find compilation library location for package 'Holism.Framework'Mollee
Also how can I view the embedded resources of a DLL? I mean I'm using ildasm to see inside a DLL package, but all I see is code, and I can't see resources.Mollee
Well, according to this GitHub comment I changed Microsoft.AspNetCore.All to version 2.0.3 and at least I don't have that error anymore. Now I have the error that cshtml layout can not be found.Mollee
L
1

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);
        });    
Lutist answered 3/7, 2018 at 16:12 Comment(0)
D
0

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.

Dowd answered 17/8, 2018 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.