Razor 视图可以编译吗?
我想知道是否可以编译 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,这是可能的。事实上,我能想到的最好的例子是电子邮件模板引擎。如果您编译并缓存模板,那么您可以快速抄袭电子邮件,而无需再次进行解析。
这也是在 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.
要编译您的观点,请执行以下操作;
单击解决方案上的项目
VS 中的资源管理器并单击“卸载”
项目
已转换为不可用
项目并单击“编辑
your_project_name.csproj”(即
将是 .vbproj 如果你
项目是VB项目)
参见以下代码;
将 MvcBuildViews 标记值从
false
更改为true
之后保存并重新加载
项目。
在构建解决方案来编译它之后,您将看到您的视图也将被编译。
注意:要测试它,请故意破坏您的视图之一中的一些代码并尝试构建。你会看到你会收到一条错误消息。
To make your views to be compiled, do the following;
clicking the project on the solution
explorer in VS and clicking unload
project
been converted to unavailable
project and click "Edit
your_project_name.csproj" (that
would be .vbproj if your
project is VB project)
see the following code;
change the MvcBuildViews tag value from
false
totrue
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.
MvcBuildViews 检查非常出色,但如果构建 Web 应用程序足够复杂,它会增加 5-10 秒的时间。它不会缓存编译输出,因此每次都会对所有视图进行完整编译。
我通过遵循上述建议并添加 Condition 属性找到了一个很好的折衷方案:
我们希望 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:
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.
是的,你可以。看看下面的帖子: 将您的 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.