Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference directly a Razor Class Library assembly

I'm working on a web application where pages need to be populated from other projects. I was hoping to use a Razor Class Library (RCL) for that. The documentation states that a RCL can be referenced only as a project reference or a nuget package. I was hoping it would just be a shortcut to make sure developers went the recommended way. Unfortunately, if I directly reference the two generated assemblies from my RCL project (MyProject.dll and MyProject.Views.dll), it doesn't work. It works only if I add a project reference to the RCL project.

The company I work for has a custom build pipeline, and for reasons outside of my control I can only directly reference assemblies.

I've spent some time comparing the output of the compilation when using direct references and project references, but I couldn't find a difference. Does anybody know why direct references don't work for RCL? If I understand what the difference is, I should be able to work my way from there.

As a bonus, is somebody aware of a workaround? Creating a CompiledRazorAssemblyPart works (as described in Loading Razor Class Libraries as plugins), but I'd rather not have to manually search for the assemblies.

like image 976
Kevin Gosse Avatar asked Sep 08 '26 05:09

Kevin Gosse


1 Answers

Put below code on Startup.cs

var assemblies = new string[] { "APP.Plugins.Test" };

        services
           .AddMvc()
           .ConfigureApplicationPartManager(apm =>
           {

               foreach (var assemblyFile in assemblies)
               {

                   //main assembly
                   var assembly = Assembly.Load(assemblyFile);
                   apm.ApplicationParts.Add(new AssemblyPart(assembly));

                   //view assembly
                   var assemblyView = Assembly.Load(assemblyFile+".Views");
                   apm.ApplicationParts.Add(new CompiledRazorAssemblyPart(assemblyView));                     
               }
           });
like image 169
tobias Avatar answered Sep 09 '26 18:09

tobias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!