NullReference
NullReference

Reputation: 1026

ASPNet Core 2.1 correct way to load precompiled views

I am trying to add a plugin like functionality to my application and having hard time making the precompiled views to be found. So lets say i have a Razor Class Library that compiled to plugin.dll and plugin.views.dll I am successfully load and add plugin.dll

Assembly PLUGIN_ASSEMBLY = null;
                            try
                            {
                                PLUGIN_ASSEMBLY = Assembly.LoadFile(PLUGIN.PluginFileName);
                                Assembly.LoadFile(PLUGIN.PluginViewsFileName);
                            }
                            catch (FileLoadException)
                            {
                                throw;
                            } 

Then the assembly is added with

MVC_BUILDER.AddApplicationPart(PLUGIN_ASSEMBLY);

Then i add the plugin base path so its normal views would be discovered

MVC_BUILDER.AddRazorOptions(o =>
                        {
                            IFileProvider physicalProvider = new PhysicalFileProvider(PLUGIN.BasePath);
                            IFileProvider compositeProvider = new CompositeFileProvider(physicalProvider);
                            o.FileProviders.Add(compositeProvider);
                        });

All above works fine except that i can only use the physically located views and not the ones from plugin.views.dll

What would be correct approach to add the views.dll and make the views discovered?

Upvotes: 1

Views: 986

Answers (1)

Sergei
Sergei

Reputation: 177

I spent all day to make it work.. and it worked.

In web app razor knew where from take precompiled views, but in console app it doesn't (maybe it my fault). Let's help him :)

At first, we need name of assembly with views:

var viewAssembly = Assembly.Load(PLUGIN_ASSEMBLY.GetName().Name + ".Views");

Second, we should create provider, that will extract all compiled views from assembly:

var viewAssemblyPart = new CompiledRazorAssemblyPart(viewAssembly);

And last, but not least - add it to collection of other providers:

MVC_BUILDER.PartManager.ApplicationParts.Add(viewAssemblyPart);

Enjoy!

Special thanks to sources from github :)

Upvotes: 3

Related Questions