asp.net-mvc 如何在运行前编译 cshtml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13482007/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:25:37  来源:igfitidea点击:

How to compile cshtml before runtime

asp.net-mvcjquery-mobilerazor

提问by FatFingers

I believe I read somewhere there is a setting in one of the project files that will allow you to compile the .cshtmlfiles when building your Visual Studio project.

我相信我在某个地方读到过一个项目文件中的设置,允许您.cshtml在构建 Visual Studio 项目时编译这些文件。

Just getting started with MVC/Razor/QueryMobile, and am getting annoyed with the "Error loading page" when I have an error in a code block inside a .cshtmlfile

刚开始使用MVC/Razor/QueryMobile,当我在.cshtml文件内的代码块中出现错误时,我对“错误加载页面”感到恼火

回答by Levi Botelho

Set <MvcBuildViews>true</MvcBuildViews>in the <PropertyGroup>element of your .csproj file.

<MvcBuildViews>true</MvcBuildViews><PropertyGroup>.csproj 文件的元素中设置。

回答by karl li

Besides the true setting, you still need to ensure below setting is active in your csproj:

除了真实设置之外,您还需要确保以下设置在您的 csproj 中处于活动状态:

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

回答by Gianpiero

MvcBuildViews is often mistakenly, as it doesn't precompile views in the terms you'd like.

MvcBuildViews 通常是错误的,因为它不会按照您想要的方式预编译视图。

MvcBuildViews builds views temporarily and gives build results back to building process to show possible build error in VS errors window.

MvcBuildViews 临时构建视图并将构建结果返回给构建过程,以在 VS 错误窗口中显示可能的构建错误。

For achieving a real precompilation of views for production release, you have to properly set specific values in the Publish Web Appconfiguration window of your project.

为了实现生产发布视图的真正预编译,您必须在项目的发布 Web 应用程序配置窗口中正确设置特定值。

See herefor the main article about how to do it, and herefor going deeper on how to do it via msBuild and Azure.

有关如何执行此操作的主要文章,请参阅此处,并在此处更深入地了解如何通过 msBuild 和 Azure 执行此操作。