asp.net-mvc 可以编译 Razor 视图吗?

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

Can Razor views be compiled?

asp.net-mvcasp.net-mvc-3compilationrazor

提问by Khalid Abuhakmeh

I was wondering if Razor views could be compiled, like WebForm based views?

我想知道是否可以编译 Razor 视图,比如基于 WebForm 的视图?

Does it even make sense to compile Razor views and why would somebody want to do that?

编译 Razor 视图是否有意义,为什么有人想要这样做?

采纳答案by Sergi Papaseit

Yes, you can. Take a look at the following post: Compile your asp.net mvc Razor views into a seperate dll

是的你可以。看看下面的帖子:将你的 asp.net mvc Razor 视图编译成一个单独的 dll

It's a "step-by-step" guide on how to compile your razor views into a separate dll. I don't know if that's what you aim to do but it'll definitely get you in the right direction.

这是关于如何将 razor 视图编译成单独的 dll 的“分步”指南。我不知道这是否是你的目标,但它肯定会让你朝着正确的方向前进。

回答by tugberk

Edit:

Here is a blog post on this topic as well:

How to Detect Errors of Our ASP.NET MVC Views on Compile Time

编辑:

这里还有一篇关于这个主题的博客文章:

如何在编译时检测 ASP.NET MVC 视图的错误

To make your views to be compiled, do the following;

要编译您的视图,请执行以下操作;

  1. Unload your project by right clicking the project on the solution explorer in VS and clicking unload project
  2. right click the project which has been converted to unavailable project and click "Edit your_project_name.csproj" (that would be .vbproj if your project is VB project)
  3. see the following code;

    <!--There some lines of code here and I deleted them to get to the point quickly-->
    
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <MvcBuildViews>false</MvcBuildViews>
    

  4. change the MvcBuildViews tag value from falseto true

  5. after that save it and reload your project.

  1. 通过在 VS 中的解决方案资源管理器上右键单击项目并单击卸载项目来卸载您的项目
  2. 右键单击已转换为不可用项目的项目,然后单击“编辑 your_project_name.csproj”(如果您的项目是 VB 项目,则为 .vbproj)
  3. 看下面的代码;

    <!--There some lines of code here and I deleted them to get to the point quickly-->
    
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <MvcBuildViews>false</MvcBuildViews>
    

  4. 将 MvcBuildViews 标记值从 更改falsetrue

  5. 之后保存并重新加载您的项目。

after you build your solution to compile it, you will see that your view will be compiled too.

在构建解决方案以对其进行编译后,您将看到您的视图也将被编译。

NOTE:to test it, break some code in one of your view on purpose and try to build. you will see that you'll get an error message.

注意:要测试它,请故意在您的一个视图中破坏一些代码并尝试构建。你会看到你会收到一条错误消息。

回答by Steve Rukuts

The MvcBuildViews check is excellent but it adds a 5-10 second penalty for building your web app if it's complex enough. It doesn't cache the compilation output so it does a full compilation of all your views every time.

MvcBuildViews 检查非常好,但如果它足够复杂,它会为构建您的 Web 应用程序增加 5-10 秒的惩罚。它不会缓存编译输出,因此每次都会对所有视图进行完整编译。

I found a nice compromise by following the above adviceand adding a Condition attribute:

通过遵循上述建议并添加 Condition 属性,我找到了一个很好的折衷方案:

<MvcBuildViews Condition=" '$(Configuration)' == 'Release' ">true</MvcBuildViews>

We'd expect ReSharper to flag up any errors in the views anyway and the developer can always build in the release configuration as a test - we have a "preflight" script that developers run so they can easily make sure that package targets work and so on - and if all that fails, the build server will catch it.

我们希望 ReSharper 无论如何都会标记视图中的任何错误,并且开发人员始终可以在发布配置中构建作为测试 - 我们有一个开发人员运行的“预检”脚本,以便他们可以轻松确保包目标工作等on - 如果所有这些都失败了,构建服务器将捕获它。

Perhaps this trick is obvious but I've only really started learning about msbuild properly as opposed to writing Powershell scripts for these tasks. I hope this is helpful to someone.

也许这个技巧很明显,但我才真正开始正确地学习 msbuild,而不是为这些任务编写 Powershell 脚本。我希望这对某人有帮助。

回答by Tejs

Yes, it's possible. In fact, the best example I can think of would be email templating engines. If you compile and cache the template, then you can quickly rip off emails without having to go through the parsing all over again.

是的,这是可能的。事实上,我能想到的最好的例子是电子邮件模板引擎。如果您编译并缓存模板,那么您可以快速窃取电子邮件,而无需重新进行解析。

That's a good example of using Razor outside of MVC as well.

这也是在 MVC 之外使用 Razor 的一个很好的例子。