How do I force compilation of ASP.NET MVC views?

后端 未结 5 1449
清酒与你
清酒与你 2020-12-05 08:12

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 a

相关标签:
5条回答
  • 2020-12-05 08:26

    Is this an initial-load issue or a steady-state issue? One issue seen is that of app pool recycling, which defaults to 20 minute timeout. If you disable timeout (or set it to something large), is that a valid workaround?

    Here's another SO answer discussing AppPool timeout and how to disable it. Basically:

    %windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00
    
    0 讨论(0)
  • 2020-12-05 08:38

    Add this to OnStart:

      using (var serverManager = new ServerManager())
            {
                string siteName = RoleEnvironment.CurrentRoleInstance.Id + "_" + "Web";
                var siteId = serverManager.Sites[siteName].Id;
                var appVirtualDir = $"/LM/W3SVC/{siteId}/ROOT";  // Do not end this with a trailing /
    
                var clientBuildManager = new ClientBuildManager(appVirtualDir, null, null,
                                            new ClientBuildManagerParameter
                                            {
                                                PrecompilationFlags = PrecompilationFlags.Default,
                                            });
    
                clientBuildManager.PrecompileApplication();
            }
    
    0 讨论(0)
  • 2020-12-05 08:39

    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.

    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.

    0 讨论(0)
  • 2020-12-05 08:46

    Take a look at Precompiled Razor Views by David Ebbo

    Why would you want to do that?

    One reason to do this is to avoid any runtime hit when your site starts, since there is nothing left to compile at runtime. This can be significant in sites with many views.

    Also, you no longer need to deploy the cshtml files at all, resulting in a smaller deployment file set.

    Another cool benefit is that it gives you the ability to unit test your views, which has always been something very difficult with the standard runtime compilation model. I’ll cover that in more details in a future post.

    0 讨论(0)
  • 2020-12-05 08:46

    If you use the Publish functionnality of Visual Studio, there is a much simpler option :

    On the Publish dialog > Settings pane, expand File Publish Options and check Precompile during publishing then click configure. On the Advanced Precompile Settings dialog box, uncheck Allow precompiled site to be updatable.

    source: https://msdn.microsoft.com/en-us/library/hh475319.aspx

    0 讨论(0)
提交回复
热议问题