Razor Class Library wwwroot content access

James Todd 1 Reputation point
2022-04-05T00:33:07.347+00:00

I have a Razor Class Library which contains some razor views, and some content within its wwwroot folder.

When I import the compiled DLL file into a .NET Core web project using Visual Studio (Add Project Reference), all works well.

However, if I attempt to load the DLL dynamically (using reflection), all seems to work apart from being able to access the content within the wwwroot folder (i.e., its not mapped to the _content/AssemblyName/ path).

I'm using the following code to import the assembly dynamically and add it as an application part:

Assembly PrimaryAssembly = Assembly.LoadFrom("bin\\Debug\\net6.0\\RCL.dll");
AssemblyPart assemblypart = new AssemblyPart(PrimaryAssembly);
EmbeddedFileProvider fileProvider = new EmbeddedFileProvider(PrimaryAssembly);
services.AddControllersWithViews().ConfigureApplicationPartManager(apm => apm.ApplicationParts.Add(assemblypart));
services.Configure<MvcRazorRuntimeCompilationOptions>(options => options.FileProviders.Add(fileProvider));

I'm using .NET Core 6.0.

Any Ideas?

Cheers.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,177 questions
{count} votes

1 answer

Sort by: Most helpful
  1. James Todd 1 Reputation point
    2022-04-07T23:47:26.84+00:00

    Hi @Zhi Lv - MSFT ,

    The contents of the CodeProject article solved it for me.

    The important piece was creating the ManifestEmbeddedFileProvider (which pointed to the Assembly, and the location that the files where (in my case "wwwroot")), then appending this fileprovider onto the rest using a CompositeFileProvider.

    That all seems to work brilliantly.

    Thanks for your help,
    James.