在构建时尝试构建 ASP MVC 视图时出错

Error trying to build ASP MVC Views at build time

我有一个 ASP MVC 4 项目。它以 MVC 1 开始,因此它使用旧式 ASPX/ASCX 视图。我想让视图在构建时编译,主要是为了进行编译时错误检查(而且重要的是,让错误直接显示在 Visual Studio 中)。我正在 Visual Studio Pro 2015 中使用 IIS Express 作为调试服务器进行开发。

根据 msdn, Haacked and questions here such as 我已经设置了以下我的 .vbproj:

<MvcBuildViews>true</MvcBuildViews>

并且还在我的 .vbproj 中创建了一个任务

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

但是,这在尝试构建时给了我一个错误:

'/temp' is not a valid IIS application.

作为一个文件,它仅引用 ASPNETCOMPILER

我尝试了各种替代方案,例如 VirtualPath - 我将项目配置为 运行 作为子目录 /local 在 dev 中,所以我试过了,.vs 文件夹中的配置中的站点名称没有任何作用。

我应该更改 VirtualPath(如果是的话更改什么)还是缺少其他一些配置才能使 temp 正常工作?

编辑

此外,我在命令行 MSBuild 中遇到了同样的错误 运行。对于我的实际生产系统,我有一个脚本,它使用命令行 MSBuild 进行构建,然后将构建的包移动到产品服务器并进行部署。因此,在构建时不会有 IIS express(或 IIS 运行ning)。您是否需要连接网络服务器来编译视图?

编辑 2

我对此处对 virtulPath 的需求以及与 Web 服务器的明显连接感到困惑。一些建议的解决方案涉及配置 IIS。 IIS Express(我用于调试)甚至不一定 运行ning 直到 一个成功的构建,据我所知?无论如何,我的应用程序 运行s 在虚拟目录中非常愉快(/local 在调试模式下)但是将其用作值不起作用。如果重要的话,这是我项目中.vs文件夹中applicationhost.config的相关部分(我尝试将相同的路径放入Documents/IIS Express中的applciationhost.config):

 <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>

所以我真的不明白为什么 VirtualPath="/" 不起作用,因为那是那些配置中引用的虚拟路径。

虽然我的主要目标是在 Visual Studio 中显示视图错误(我会接受仅限于此的答案),但我在一台机器上使用 MSBuild 为生产环境构建的情况也是如此甚至不必安装 IIS。

AspNetCompiler 调用 aspnet_compiler.exe (https://msdn.microsoft.com/en-us/library/ms229863.aspx?f=255&MSPPError=-2147217396)。在 Visual Studio 错误列表中,对此 exe 的调用显示为 ASPNETCOMPILER。

我建议您启用 诊断级别日志记录 https://msdn.microsoft.com/en-us/library/jj651643.aspx,然后在构建日志中搜索 aspnet_compiler.exe 字符串并检查调用 aspnet_compiler.exe 时使用的参数(特别是什么路径)。您也可以从日志中复制并在 cmd.exe.

中手动调用 aspnet_compiler.exe

很可能问题出在 PhysicalPath 属性中,而不是在 VirtualPath 中,似乎 VirtualPath 属性不影响构建。检查这些答案:

  • 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

如果您需要 MSBuild 和 Visual Studio 的单独路径,您可以使用此技术 http://web4.codeproject.com/Articles/156989/Resolve-temp-global-asax-error-ASPPARSE-Could?display=Print

根据@snautz 的回答,我实际使用的对我有用的行是

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

许多在线示例使用 $(ProjectDir)\..$(ProjectName) 作为 PhysicalPath 但我不确定为什么,因为它只是解析为 $(ProjectDir) ...