Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error trying to build ASP MVC Views at build time

I have an ASP MVC 4 project. It started as MVC 1, so it uses the old-style ASPX/ASCX views. I want to have the views compile at build time, mainly to get compile time error checking (and also, importantly, to have the errors show directly in Visual Studio). I'm developing in Visual Studio Pro 2015 using IIS Express as debug server.

As per both msdn, Haacked and questions here such as this I have set the following my .vbproj:

<MvcBuildViews>true</MvcBuildViews>

and also in my .vbproj created a task

<Target Name="BuildViews" Condition="'$(MvcBuildViews)'=='true'" AfterTargets="Build">
  <Message Importance="normal" Text="Precompiling views" />
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

However, that gives me an error when trying to build:

'/temp' is not a valid IIS application.

As a file it just references ASPNETCOMPILER

I've tried various alternatives as VirtualPath - I have the project configured to run as a sub-directory /local in dev so I tried that, and the site names from the config in the .vs folder and nothing works.

Should I change the VirtualPath (and if so to what) or is there some other config missing for temp to work?

Edit

Also, I get the same error running a command line MSBuild. For my actual production systems, I have a script which does a build using command line MSBuild then moves the built package to the product servers and deploys. So that won't have either IIS express (or IIS running) when it's built. Do you need a web server connected just to compile the views?

Edit 2

I am confused by the need for the virtulPath here and the apparent connection to the web server. Some of the suggested solutions involve configuring IIS. IIS Express (which I use for debugging) isn't even necessarily running until after a successful build as I understand it? Anyway, my application runs in a virtual directory quite happily (/local in debug mode) but using that as a value doesn't work. If it matters, this is the relevant part of the applicationhost.config in the .vs folder in my project (I tried putting the same paths into the applciationhost.config in Documents/IIS Express):

 <site name="CarWeb-Site" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Users\adam.conway\Documents\My Web Sites\CarWeb-Site" />
    </application>
    <application path="/local" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="filesystem path to my project" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:57047:localhost" />
        <binding protocol="http" bindingInformation="*:57047:*" />
    </bindings>
</site>

So I really don't understand why just VirtualPath="/" doesn't work, since that's the virtual path referenced in those configs.

While my primary aim here is showing view errors in Visual Studio (and I would accept an answer limited to that) it is also the case that I build for production environments using MSBuild on a machine which doesn't necessarily even have IIS installed.

like image 680
Adam Avatar asked Feb 23 '17 07:02

Adam


2 Answers

AspNetCompiler calls aspnet_compiler.exe (https://msdn.microsoft.com/en-us/library/ms229863.aspx?f=255&MSPPError=-2147217396). In Visual Studio Error List calls to this exe are displayed as ASPNETCOMPILER.

I recommend you to enable diagnostic level logging https://msdn.microsoft.com/en-us/library/jj651643.aspx, then search build log for aspnet_compiler.exe string and check what parameters (expecially what path) are being used when calling aspnet_compiler.exe. Also you can copy from log & manually call aspnet_compiler.exe in cmd.exe.

Most probably problem is in PhysicalPath attribute, not in VirtualPath, it seems VirtualPath attribute does not affect build. Check those answers:

  • VirtualPath in AspNetCompiler MSBuild Task - does it have to be equal to the final deployed Virtual Path?
  • Why do I need VirtualPath property for AspNetCompiler task

If you need separate paths for MSBuild and Visual Studio you can use this technique http://web4.codeproject.com/Articles/156989/Resolve-temp-global-asax-error-ASPPARSE-Could?display=Print

like image 164
snautz Avatar answered Nov 19 '22 18:11

snautz


Based on @snautz answer, the line I actually use that works for me is

<AspNetCompiler VirtualPath="/" PhysicalPath="$(ProjectDir)" />

Many of the online examples used $(ProjectDir)\..\$(ProjectName) as the PhysicalPath but I'm not sure why, since that just resolves to $(ProjectDir) ...

like image 26
Adam Avatar answered Nov 19 '22 19:11

Adam