I want an msbuild task to compile the views so I can see if there are compile time errors at well... compile time. Any ideas?
我想要一个msbuild任务来编译视图,这样我就可以看到是否存在编译时错误……编译时间。什么好主意吗?
8 个解决方案
#1
128
I frankly would recommend the RazorGenerator nuget package. That way your views have a .designer.cs
file generated when you save them and on top of getting compile time errors for you views, they are also precompiled into the assembly (= faster warmup) and Resharper provides some additional help as well.
坦白地说,我推荐RazorGenerator nuget包。这样,您的视图在保存时就会生成.designer.cs文件,在为视图获取编译时间错误的同时,还会将它们预编译到程序集中(=更快的预热),Resharper还会提供一些额外的帮助。
To use this include the RazorGenerator nuget package in you ASP.NET MVC project and install the "Razor Generator" extension under item under Tools → Extensions and Updates.
要使用这个包括RazorGenerator nuget包在您的ASP中。净MVC项目并安装“剃刀发电机”项下扩展工具→扩展和更新。
We use this and the overhead per compile with this approach is much less. On top of this I would probably recommend .NET Demon by RedGate which further reduces compile time impact substantially.
我们使用这种方法,使用这种方法进行每次编译的开销要小得多。除此之外,我可能还会推荐。net Demon,它可以大大降低编译时间的影响。
Hope this helps.
希望这个有帮助。
#2
522
From the readme word doc for RC1 (not indexed by google)
来自RC1的readme word doc(未被谷歌索引)
ASP.NET Compiler Post-Build Step
ASP。净编译器Post-Build一步
Currently, errors within a view file are not detected until run time. To let you detect these errors at compile time, ASP.NET MVC projects now include an MvcBuildViews property, which is disabled by default. To enable this property, open the project file and set the MvcBuildViews property to true, as shown in the following example:
目前,直到运行时,才会检测到视图文件中的错误。要让您在编译时检测这些错误,请使用ASP。NET MVC项目现在包含一个mvcbuildview属性,默认情况下是禁用的。要启用此属性,请打开项目文件并将MvcBuildViews属性设置为true,如下面的示例所示:
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>
Note Enabling this feature adds some overhead to the build time.
注意,启用此特性会增加构建时间的开销。
You can update projects that were created with previous releases of MVC to include build-time validation of views by performing the following steps:
您可以通过执行以下步骤来更新之前发布的MVC版本的项目,包括构建时间验证视图:
- Open the project file in a text editor.
- 在文本编辑器中打开项目文件。
- Add the following element under the top-most
<PropertyGroup>
element:<MvcBuildViews>true</MvcBuildViews>
-
将以下元素添加到最顶层
元素: true 。 - At the end of the project file, uncomment the
<Target Name="AfterBuild">
element and modify it to match the following: -
在项目文件结束时,取消对
元素的注释,并对其进行修改以匹配以下内容:
<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
</Target>
#3
41
You can use aspnet_compiler for this:
您可以使用aspnet_compiler:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler -v /Virtual/Application/Path/Or/Path/In/IIS/Metabase -p C:\Path\To\Your\WebProject -f -errorstack C:\Where\To\Put\Compiled\Site
where "/Virtual/Application/Path/Or/Path/In/IIS/Metabase" is something like this: "/MyApp" or "/lm/w3svc2/1/root/"
其中“/Virtual/Application/Path/Or/Path/In/IIS/Metabase”如下:“/MyApp”或“/lm/w3svc2/1/root/”
Also there is a AspNetCompiler Task on MSDN, showing how to integrate aspnet_compiler with MSBuild:
另外,MSDN上还有一个AspNetCompiler任务,显示如何将aspnet_compiler与MSBuild集成:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="PrecompileWeb">
<AspNetCompiler
VirtualPath="/MyWebSite"
PhysicalPath="c:\inetpub\wwwroot\MyWebSite\"
TargetPath="c:\precompiledweb\MyWebSite\"
Force="true"
Debug="true"
/>
</Target>
</Project>
#4
24
Also, if you use Resharper, you can active Solution Wide Analysis and it will detect any compiler errors you might have in aspx files. That is what we do...
此外,如果您使用Resharper,您可以主动进行解决方案范围的分析,它会检测出您在aspx文件中可能存在的任何编译错误。这就是我们所做的……
#5
11
Next release of ASP.NET MVC (available in January or so) should have MSBuild task that compiles views, so you might want to wait.
下一版本的ASP。NET MVC(1月份左右可用)应该有编译视图的MSBuild任务,所以您可能需要等待。
See announcement
看到公告
#6
6
The answer given here works for some MVC versions but not for others.
这里给出的答案适用于某些MVC版本,但不适用于其他版本。
The simple solution worked for MVC1 but on upgrading to MVC2 the views were no longer being compliled. This was due to a bug in the website project files. See this Haacked article.
这个简单的解决方案适用于MVC1,但在升级到MVC2时,视图不再复杂。这是由于网站项目文件中的一个错误。看到哈克的这篇文章。
See this: http://haacked.com/archive/2011/05/09/compiling-mvc-views-in-a-build-environment.aspx
看到这个:http://haacked.com/archive/2011/05/09/compiling-mvc-views-in-a-build-environment.aspx
#7
3
Build > Run Code Analysis
构建>运行代码分析
Hotkey : Alt+F11
热键:Alt +季
Helped me catch Razor errors.
帮助我发现剃刀错误。
#8
1
Using Visual Studio's Productivity Power Tools (free) extension helps a bit. Specifically, the Solution Error Visualizer
feature. With it, compilation errors marked visually in the solution explorer (in the source file where the error was found). For some reason, however, this feature does not work as with other errors anywhere else in the code.
使用Visual Studio的生产力工具(免费)扩展会有所帮助。具体地说,解决方案错误的可视化特性。使用它,可以在解决方案资源管理器(在发现错误的源文件中)可视化地标记编译错误。但是,由于某些原因,这个特性不能像代码中其他地方的错误那样工作。
With MVC views, any compile-time errors will still be underlined in red in their respective .cs files, but signaling these errors is not propagated upwards in the Solution Explorer (in no way, even not in the containing source file).
对于MVC视图,任何编译时错误都将在各自的.cs文件中以红色下划线,但是在解决方案资源管理器中(在任何情况下,甚至不在包含源文件中)都不会向上传播这些错误。
Thanks for BlueClouds
for correcting my previous statement.
感谢BlueClouds更正我之前的声明。
I have just reported this as an issue on the extension's github project.
我刚刚将这作为扩展项目github的一个问题进行了报告。
#1
128
I frankly would recommend the RazorGenerator nuget package. That way your views have a .designer.cs
file generated when you save them and on top of getting compile time errors for you views, they are also precompiled into the assembly (= faster warmup) and Resharper provides some additional help as well.
坦白地说,我推荐RazorGenerator nuget包。这样,您的视图在保存时就会生成.designer.cs文件,在为视图获取编译时间错误的同时,还会将它们预编译到程序集中(=更快的预热),Resharper还会提供一些额外的帮助。
To use this include the RazorGenerator nuget package in you ASP.NET MVC project and install the "Razor Generator" extension under item under Tools → Extensions and Updates.
要使用这个包括RazorGenerator nuget包在您的ASP中。净MVC项目并安装“剃刀发电机”项下扩展工具→扩展和更新。
We use this and the overhead per compile with this approach is much less. On top of this I would probably recommend .NET Demon by RedGate which further reduces compile time impact substantially.
我们使用这种方法,使用这种方法进行每次编译的开销要小得多。除此之外,我可能还会推荐。net Demon,它可以大大降低编译时间的影响。
Hope this helps.
希望这个有帮助。
#2
522
From the readme word doc for RC1 (not indexed by google)
来自RC1的readme word doc(未被谷歌索引)
ASP.NET Compiler Post-Build Step
ASP。净编译器Post-Build一步
Currently, errors within a view file are not detected until run time. To let you detect these errors at compile time, ASP.NET MVC projects now include an MvcBuildViews property, which is disabled by default. To enable this property, open the project file and set the MvcBuildViews property to true, as shown in the following example:
目前,直到运行时,才会检测到视图文件中的错误。要让您在编译时检测这些错误,请使用ASP。NET MVC项目现在包含一个mvcbuildview属性,默认情况下是禁用的。要启用此属性,请打开项目文件并将MvcBuildViews属性设置为true,如下面的示例所示:
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>
Note Enabling this feature adds some overhead to the build time.
注意,启用此特性会增加构建时间的开销。
You can update projects that were created with previous releases of MVC to include build-time validation of views by performing the following steps:
您可以通过执行以下步骤来更新之前发布的MVC版本的项目,包括构建时间验证视图:
- Open the project file in a text editor.
- 在文本编辑器中打开项目文件。
- Add the following element under the top-most
<PropertyGroup>
element:<MvcBuildViews>true</MvcBuildViews>
-
将以下元素添加到最顶层
元素: true 。 - At the end of the project file, uncomment the
<Target Name="AfterBuild">
element and modify it to match the following: -
在项目文件结束时,取消对
元素的注释,并对其进行修改以匹配以下内容:
<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
</Target>
#3
41
You can use aspnet_compiler for this:
您可以使用aspnet_compiler:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler -v /Virtual/Application/Path/Or/Path/In/IIS/Metabase -p C:\Path\To\Your\WebProject -f -errorstack C:\Where\To\Put\Compiled\Site
where "/Virtual/Application/Path/Or/Path/In/IIS/Metabase" is something like this: "/MyApp" or "/lm/w3svc2/1/root/"
其中“/Virtual/Application/Path/Or/Path/In/IIS/Metabase”如下:“/MyApp”或“/lm/w3svc2/1/root/”
Also there is a AspNetCompiler Task on MSDN, showing how to integrate aspnet_compiler with MSBuild:
另外,MSDN上还有一个AspNetCompiler任务,显示如何将aspnet_compiler与MSBuild集成:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="PrecompileWeb">
<AspNetCompiler
VirtualPath="/MyWebSite"
PhysicalPath="c:\inetpub\wwwroot\MyWebSite\"
TargetPath="c:\precompiledweb\MyWebSite\"
Force="true"
Debug="true"
/>
</Target>
</Project>
#4
24
Also, if you use Resharper, you can active Solution Wide Analysis and it will detect any compiler errors you might have in aspx files. That is what we do...
此外,如果您使用Resharper,您可以主动进行解决方案范围的分析,它会检测出您在aspx文件中可能存在的任何编译错误。这就是我们所做的……
#5
11
Next release of ASP.NET MVC (available in January or so) should have MSBuild task that compiles views, so you might want to wait.
下一版本的ASP。NET MVC(1月份左右可用)应该有编译视图的MSBuild任务,所以您可能需要等待。
See announcement
看到公告
#6
6
The answer given here works for some MVC versions but not for others.
这里给出的答案适用于某些MVC版本,但不适用于其他版本。
The simple solution worked for MVC1 but on upgrading to MVC2 the views were no longer being compliled. This was due to a bug in the website project files. See this Haacked article.
这个简单的解决方案适用于MVC1,但在升级到MVC2时,视图不再复杂。这是由于网站项目文件中的一个错误。看到哈克的这篇文章。
See this: http://haacked.com/archive/2011/05/09/compiling-mvc-views-in-a-build-environment.aspx
看到这个:http://haacked.com/archive/2011/05/09/compiling-mvc-views-in-a-build-environment.aspx
#7
3
Build > Run Code Analysis
构建>运行代码分析
Hotkey : Alt+F11
热键:Alt +季
Helped me catch Razor errors.
帮助我发现剃刀错误。
#8
1
Using Visual Studio's Productivity Power Tools (free) extension helps a bit. Specifically, the Solution Error Visualizer
feature. With it, compilation errors marked visually in the solution explorer (in the source file where the error was found). For some reason, however, this feature does not work as with other errors anywhere else in the code.
使用Visual Studio的生产力工具(免费)扩展会有所帮助。具体地说,解决方案错误的可视化特性。使用它,可以在解决方案资源管理器(在发现错误的源文件中)可视化地标记编译错误。但是,由于某些原因,这个特性不能像代码中其他地方的错误那样工作。
With MVC views, any compile-time errors will still be underlined in red in their respective .cs files, but signaling these errors is not propagated upwards in the Solution Explorer (in no way, even not in the containing source file).
对于MVC视图,任何编译时错误都将在各自的.cs文件中以红色下划线,但是在解决方案资源管理器中(在任何情况下,甚至不在包含源文件中)都不会向上传播这些错误。
Thanks for BlueClouds
for correcting my previous statement.
感谢BlueClouds更正我之前的声明。
I have just reported this as an issue on the extension's github project.
我刚刚将这作为扩展项目github的一个问题进行了报告。