asp.net-mvc razor 视图没有给出编译时错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16052956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
razor views are not giving compile time error
提问by Amit Andharia
I've recently installed VS 2012 Professional.
我最近安装了 VS 2012 Professional。
When i try to build my MVC4 Web Project. It doesn't recognize error in razor view when i do "Build" or "Re-Build".
当我尝试构建我的 MVC4 Web 项目时。当我执行“构建”或“重新构建”时,它无法识别剃刀视图中的错误。
Example:
I removed a namespace from the project / or say renamed it. i build the solution, it gave me errors in all cs files, which i fixed by changing the namespace. The entire solution build successfully. When i run the project it gave me Compilation Errorsaying that namespace not found, because the old namespace was still referred in some of the views (*.cshtml files).
示例:
我从项目中删除了一个命名空间/或者说重命名了它。我构建了解决方案,它在所有 cs 文件中都给了我错误,我通过更改命名空间来修复这些错误。整个解决方案构建成功。当我运行该项目时,它给了我编译错误,说找不到命名空间,因为在某些视图(*.cshtml 文件)中仍然引用了旧的命名空间。
Expected Solution:
I wish when i do "Build" or "Re-Build", it should recognize such errors and show me along with any other errors.
预期的解决方案:
我希望当我执行“构建”或“重新构建”时,它应该识别此类错误并将任何其他错误显示给我。
This was working fine with VS 2010, am i missing any configuration?
这在 VS 2010 中运行良好,我是否缺少任何配置?
Thanks In Advance !! Amit
提前致谢 !!阿米特
EditI found the answer myself, i think it was early to post the question :
编辑我自己找到了答案,我认为发布问题还为时过早:
razor syntax with errors compiles when it should not compile
Another Problem
After changing value to True in .csproject file, when i start building the project it shows error, but it shows only one error at a time. let's say, i've 5 errors in total 3 views. it would just show me one error. Is there any solution so that it shows all the 5 errors ?
另一个问题
在 .csproject 文件中将值更改为 True 后,当我开始构建项目时,它显示错误,但一次只显示一个错误。比方说,我在总共 3 次观看中有 5 个错误。它只会告诉我一个错误。是否有任何解决方案可以显示所有 5 个错误?
回答by Darin Dimitrov
When i try to build my MVC4 Web Project. It doesn't recognize error in razor view when i do "Build" or "Re-Build".
当我尝试构建我的 MVC4 Web 项目时。当我执行“构建”或“重新构建”时,它无法识别剃刀视图中的错误。
Seems normal. Razor views are dynamically compiled by the ASP.NET runtime. If you want your views to be built at compile-time you could add the following option to your .csprojfile:
看起来很正常。Razor 视图由 ASP.NET 运行时动态编译。如果您希望在编译时构建视图,您可以将以下选项添加到您的.csproj文件中:
<PropertyGroup>
<MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>
You may take a look at the this articlefor more details.
您可以查看这篇文章了解更多详情。
回答by Francisco Goldenstein
I recommend you to add MmvcBuildViews tag as a child of the Release PropertyGroup tag so the views are only compiled when you compile in Release mode (or when you publish in Release mode). This way, your app is faster when you are debugging. And, you get compile-time checks before deploying (when you build on Release mode). In summary, you get the best of both worlds.
我建议您将 MmvcBuildViews 标记添加为 Release PropertyGroup 标记的子项,以便仅在您在 Release 模式下编译(或在 Release 模式下发布时)编译视图。这样,您的应用程序在调试时会更快。而且,您在部署之前(当您在发布模式下构建时)会进行编译时检查。总之,您可以两全其美。
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>
回答by karl li
According to my experience, besides the true setting mentioned above, you still need to ensure below setting exist in your csproj file:
根据我的经验,除了上面提到的真实设置之外,您还需要确保您的 csproj 文件中存在以下设置:
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
回答by Dmitry Efimenko
By default it does not compile views. You can enable this feature, but keep in mind that it will increase build time.
默认情况下,它不编译视图。您可以启用此功能,但请记住,它会增加构建时间。
You can enable compiling view by following these steps:
您可以按照以下步骤启用编译视图:
- Unload project
- Open project file
- Find
<MvcBuildViews>false</MvcBuildViews>and change it to havetrue - Close project file and reload project
- 卸载项目
- 打开项目文件
- 查找
<MvcBuildViews>false</MvcBuildViews>并更改它以拥有true - 关闭项目文件并重新加载项目
回答by vishal joshi
Its really Very Simple Answer Go through this Steps:
它真的非常简单的答案通过以下步骤:
Unload Project
卸载项目
Edit Project
编辑项目
Than Search :
比搜索:
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
below add
下面添加
<Target Name="AfterBuild" Condition="'$(Configuration)'!='Debug'">
<RemoveDir Directories="$(BaseIntermediateOutputPath)" />
</Target>
That's it Than Unload Project . Build
这就是比卸载项目。建造
And Check Ur Syntax Is work properly .
并检查 Ur Syntax 是否正常工作。
I am Sure.
我相信。

