Html.RenderPartial slow

Our web application contains dozens of partial views, some of them are children of others. We noticed that the application comes slowly at the first boot, it takes from 0.5 to 1 second to initialize each view that it calls for the first time. I kept track of time and found that this:

Html.RenderPartial("~/Full/Relative/Path/To/View.cshtml", null); 

may take about 1 second, even if the view is completely empty. At the same time it is:

 var view = ViewEngines.Engines.FindPartialView( ViewContext.Controller.ControllerContext, "~/Full/Relative/Path/To/View.cshtml"); 

takes 1 millisecond, so no time is spent searching for a file.
Questions:
1. Is it normal for a view compiler to take so long?
2. Is there any other way to quickly make the first call, but not have a representation precompiled with the directive in csproj?

ps: it is strange that the same views loaded faster at the beginning of application development.

+5
source share
3 answers

The delay you see boils down to the initial compilation of the view on first use. The speed will be lower than the server (processor and drive speed, memory, usage, etc.).

You either live with it, or set the project to precompile views to avoid compiling at runtime.

+7
source

I will also add that:

 Html.RenderPartial("~/Full/Relative/Path/To/View.cshtml", null); 

Actually rendering html to stream. Where how:

 var view = ViewEngines.Engines.FindPartialView( ViewContext.Controller.ControllerContext, "~/Full/Relative/Path/To/View.cshtml"); 

returns only IView ; it does not execute the view.

+1
source

Well, TrueBlueAussie's answer is correct, but I have something to add.
When compiling the views (during compilation of the application or later, depending on the configuration of the project), the compiler creates * .dll, one for each folder containing the views. When an application is about to display a view for the first time, the entire DLL containing this view is loaded by the IIS server. You can easily find that it is watching the output window in VS during this process, and this may take a second or even more. After loading the DLL, all other views located in the same folder are displayed quickly. Thus, the more folders in the views, the more often you download them to IIS and spend time. Hope this helps.

0
source

All Articles