0

I would like to be able to use strongly-typed Razor views with models and controllers loaded at runtime. The assemblies are loaded during application startup in ConfigureServices via anIApplicationFeatureProvider.

        services.AddMvc()
           .ConfigureApplicationPartManager(apm => apm.FeatureProviders.Add(new DynamicControllerFeatureProvider()));

Views with a model type of dynamic will compile just fine, but any strongly typed Razor views do not compile.

The type or namespace name 'TestModules' does not exist in the namespace 'TestProject' (are you missing an assembly reference?)

How can I make the Razor engine aware of the dynamically loaded assemblies so I can use strongly typed views? It seems like using a custom RazorBuildProvider would work, but is there a better aproach?

Brian
  • 6,910
  • 8
  • 44
  • 82

1 Answers1

1

Razor views are precompiled by default. Since the necessary assemblies aren't available until runtime, precompilation is impossible. You'll need to turn it off with the following in your csproj:

<PropertyGroup>
    <RazorCompileOnBuild>false</RazorCompileOnBuild>
    <RazorCompileOnPublish>false</RazorCompileOnPublish>
</PropertyGroup>
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • That kind of make sense, but I am not even sure that my project was was set to pre-compile in the first place. It was compiling just fine and the error only occured when attempting to render the view. After explicitly disabling those properties, it's still using ONLY the assemblies referenced in the project and not the dynamically loaded ones. Did this work for you? I get the same error message as indicated above. – Brian Jan 08 '19 at 22:53