4

Using VS2015 and MVC 5.2.3, I am trying to shift areas of functionality including Razor views to class libraries, so that I can share it between multiple projects.

I use the MvcBuildViews facility as typically described, and setup like the following, as it often catches typos before publishing:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
        <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)" />
</Target>

This works fine with the main project itself, but I am having trouble getting it to work with the libraries.Instead, I get the following compiler error:

web.config(41): error ASPCONFIG: Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

The referenced web.config section appears as below:

<system.web>
    <compilation>
        <assemblies>
            <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />                
        </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.6"/>
</system.web>

All settings look very similar to the main project, and experimentation has been fruitless and keeps leading me back to this same basic error above. Required references are all set and the same as for the main project.

I understand the AspNetCompiler tag in the csproj is a wrapper for the aspnet_compiler.exe process, but I can't see what is different about the compilation environment in the class library compared to the main project.

Might it be possible to pass a specific assembly search folder to the compiler in this tag? - since the System.Web.Mvc.dll assembly certainly does exist, in the bin\Release folder of the class library project. Or is there a better approach?

Detailed information on the internal processes of aspnet_compiler.exe is scarce and it does not seem to have a verbose diagnostics mode.

How should I proceed with this?

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
stephen
  • 303
  • 1
  • 8
  • As an Alternative, you can use [RazorEngine](https://github.com/Antaris/RazorEngine) to render your view into HtmlString and share the HTML between projects, [see this answer](https://stackoverflow.com/questions/53494349/does-razor-viewengine-cache-the-rendered-htmls/53640039#53640039) for more information. – Hooman Bahreini Feb 26 '19 at 00:27
  • Thanks Hooman, I have looked at RazorEngine, but I am trying to minimise third-party dependencies in projects, and I feel it must be possible to get this working natively. If MvcBuildViews works in main projects, why not in libraries? – stephen Feb 26 '19 at 19:01

0 Answers0