如何强制编译 ASP.NET MVC 视图? [英] How do I force compilation of ASP.NET MVC views?

查看:23
本文介绍了如何强制编译 ASP.NET MVC 视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows Azure Web 角色,其中包含一个使用 ASP.NET MVC 的网站.当 HTTP 请求到达并首次加载页面时,会编译视图(.aspx 或 .cshtml),这需要一些时间,因此第一次提供页面所需的时间明显长于稍后提供相同页面的时间.

I have a Windows Azure web role that contains a web site using ASP.NET MVC. When an HTTP request arrives and a page is first loaded the view (.aspx or .cshtml) is compiled and that takes some time and so the first time a page is served it takes notable longer than later serving the same page.

我已启用 <MvcBuildViews>(在此答案中在此答案中描述)以强制执行视图的编译时验证,但这似乎对部署和运行站点时的编译没有任何影响.

I've enabled <MvcBuildViews> (described in this answer) to enforce compile-time validation of views, but that doesn't seem to have any effect on their compilation when the site is deployed and running.

Azure web 角色有所谓的启动任务,还有一个特殊的 OnStart() 方法,我可以在其中放置任何预热代码,所以一旦我知道该怎么做,将它添加到角色中就不是一个问题.

Azure web roles have so-called startup tasks and also a special OnStart() method where I can place whatever warmup code, so once I know what to do adding that into the role is not a problem.

有没有办法强制编译所有视图?

Is there a way to force compilation of all views?

推荐答案

原来有 ASP.NET 预编译 可以使用 ClientBuildManager.PrecompileApplication 并模仿按需编译行为,但只编译每个页面.尝试过 - 第一次加载看起来明显更快.

Turns out there's ASP.NET Precompilation that can be performed using ClientBuildManager.PrecompileApplication and mimics the on-demand compilation behavior, but just compiles every page. Tried it - the first load looks notably faster.

重要的部分是作为 ClientBuildManager 构造函数参数传递的内容.解决方案是枚举 Site 对象的所有 .Applications 并为 .Applications 中的每个项目枚举所有 .VirtualDirectories> 并使用每个项目中的 PathVirtualPath 作为 ClientBuildManager 构造函数的参数.

The non-trivial part is what to pass as ClientBuildManager constructor parameters. The solution is to enumerate all .Applications of the Site object and for each item in .Applications enumerate all .VirtualDirectories and use Path and VirtualPath from each item as parameters to ClientBuildManager constructor.

这篇关于如何强制编译 ASP.NET MVC 视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆