Razor 视图可以编译吗?

发布于 2024-10-28 03:10:05 字数 83 浏览 2 评论 0原文

我想知道是否可以编译 Razor 视图,就像基于 WebForm 的视图一样?

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆躺在深渊里 2024-11-04 03:10:06

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

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

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.

农村范ル 2024-11-04 03:10:05

编辑:

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

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

要编译您的观点,请执行以下操作;

  1. 正确卸载您的项目
    单击解决方案上的项目
    VS 中的资源管理器并单击“卸载”
    项目
  2. 右键单击具有
    已转换为不可用
    项目并单击“编辑
    your_project_name.csproj”(即
    将是 .vbproj 如果你
    项目是VB项目)
  3. 参见以下代码;

    
    
    v4.0
    
    

  4. 将 MvcBuildViews 标记值从 false 更改为 true

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

在构建解决方案来编译它之后,您将看到您的视图也将被编译。

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

Edit:

Here is a blog post on this topic as well:

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

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 false to true

  5. after that save it and reload your
    project.

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.

心头的小情儿 2024-11-04 03:10:05

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

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

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

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

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

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.

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

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

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.

老子叫无熙 2024-11-04 03:10:05

是的,你可以。看看下面的帖子: 将您的 asp.net mvc Razor 视图编译到单独的 dll 中

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

Yes, you can. Take a look at the following post: Compile your asp.net mvc Razor views into a seperate 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文