2

I have a Razor Class Library which is used as a reference in the ASP.NET Core MVC project. The app is working fine. I removed the reference and used CompiledRazorAssemblyPart to add the dll to the application parts. Here is a sample code Loading Razor Class Libraries as plugins

The same route that worked when I used the RCL as a reference wont work anymore. Should I use any other settings to load the view?

wonderful world
  • 10,969
  • 20
  • 97
  • 194
  • I have the exact same question! Normal .cshtml (/Views/) files are working perfect. But RazorPages give me a "FileNotFound" Exception when using a dll instead of a project reference. – serious Jul 14 '18 at 23:13

1 Answers1

5

I had a similar issue with using CompiledRazorAssemblyPart, and it turns out the problem was that I was adding my RCL assembly (e.g. MyPlugin.dll) as a CompiledRazorAssemblyPart, whereas you need to add the .Views assembly (e.g. MyPlugin.Views.dll). Below is an example of the code I'm now using to add a plugin assembly that is not referenced by the main web application project. Note that you probably still want the plugin assembly as a standard application part for controllers etc.

public static void AddPluginApplicationPart(IMvcBuilder mvcBuilder, Assembly assembly)
        {
            mvcBuilder.AddApplicationPart(assembly);

            Assembly viewsAssembly = null;
            try
            {
                viewsAssembly = Assembly.Load(assembly.GetName().Name + ".Views");
            }
            catch (FileNotFoundException)
            {
            }

            if (viewsAssembly != null)
            {
                // Adding this application part allows us to use compiled razor views from this plugin
                var viewsApplicationPart = new CompiledRazorAssemblyPart(viewsAssembly);
                mvcBuilder.ConfigureApplicationPartManager(manager => manager.ApplicationParts.Add(viewsApplicationPart));
            }
        }
Andy
  • 541
  • 6
  • 9