How Do I Compile ASP.NET Views After Deployment
I have a complex site running Sitecore composed of both ASP.NET Webforms (.aspx, .ascx) views and Razor (.cshtml) views, and would like to get them all compiled after deployment so there’s no runtime compilation performance hit the first request for anything.
I’ve tried running aspnet_compiler against the site, but
- It’s telling me it can’t find an assembly which is 100% in the bin folder
- Failure to compile one view shouldn’t stop it from compiling others
I’m now looking into how views are compiled at runtime on demand, and have found
System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath) so would I be able to scan my website folder for all views and call that method for each one?
I’m not looking to precompile views, but just compile them after deployment as if there had been requests for pages requiring them. How can this be done?
Solution – 1
I ended up using
System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath), and it works really well. As that method takes virtual paths, I found the full paths of views I want compiled, then subtracted
Server.MapPath to get the virtual path.