ASP.Net MVC MvcBuildViews increasing compile time

2020-06-13 11:12发布

问题:

We have a project and would like to build the views to generate compile time errors if there is anything wrong within the Views' .cshtml files.

Still, compile time increases drastically:

  • MvcBuildViews = true takes 62 seconds
  • MvcBuildViews = false takes 9 seconds

Is this something acceptable? Cause the increase is quite drastic in which we cannot stand having to wait for such compile times. Any way we can improve such compilation?

The project up till now consists around 130 Views & Partial Views (.cshtml files). Is this considered to be large / medium / small?

回答1:

Well I think having the possibility to compile the views is a good thing by itself but I cannot wait that long either. So what I prefer to do is to add that MvcBuildViews = true inside the ProperyGroup of Release so that you only Compile the views at the Release time and before deployment

The property group should look like this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> 

So put your

<MvcBuildViews>true</MvcBuildViews>

Inside this Release block. this way you still compile your views once and not every time you try to debug...



回答2:

We struggled with the same problem. We began compiling Views in order to catch obvious issues that would creep up on us during Integration Tests and UX Tests. Even worse were the bugs that somehow crept into production.

But, as you noticed, our build times became intolerable. Like you, our developers build countless times and that became a major part of our day. We had jokes about testing after lunch so the build could be done while we were out.

We eventually moved to building before UX-tests.

Now we are moving toward pre-compiling. Only one guy on our team has adopted it for now and apparently pre-compiles are noticably better than builds (incremental vs total). And setup is basically a nuget fetch.

These articles should be a good start

http://stacktoheap.com/blog/2013/01/19/precompiling-razor-views-in-asp-dot-net-mvc-3/

http://blog.davidebbo.com/2011/06/precompile-your-mvc-views-using.html



回答3:

Set MvcBuildViews to false in the csproj for the debug config.

<MvcBuildViews Condition=" '$(Configuration)' != 'Debug' ">true</MvcBuildViews>

Add the following external command in the menu "Tools/External tools"

Title: Compile with MVC views
Command: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe
Arguments: $(SolutionDir)$(SolutionFileName) /p:MvcBuildViews=true
Initial directory: $(SolutionDir) 
Use Output window:  checked

Then add a shortcut to that: Go in tools/options/environnement/keyboard Locate the command "Tools.ExternalCommandX" where X is the position of your external command where you just added it. Assign a key (Ex: Ctrl-shift-1)

Now the explanations: We have the same problem. We don't want to compile views most of the time but we like to compile them before checking in the code, this way we compile views 1 time out of 50 or something. To acheive this, I added an external tool command to msbuild with the mvcbuildviews parameter. The only drawback is that the errors aren't listed in the errors window of visual studio, you have to look at the output window and double click on errors if any.