Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precompile asp.net views with ms build

When I deploy asp.net application through visual studio I know I can just check Precompile during publish and uncheck Allow precompiled site to be updateable.

And I want to do the same with msbuild tool and I am using /p:MvcBuildViews=true /p:EnableUpdateable=false but when I go to IIS and open views they still have their content which means they are not precompiled, right?

They should have line This is a marker file generated by the precompilation tool as they do when publishing from VS. Am I missing something?

like image 339
Aleksa Avatar asked Feb 28 '18 11:02

Aleksa


People also ask

What ASP NET deployment method is used to Precompile website content before installation?

Performing Precompilation You can precompile a Web site using the Aspnet_compiler.exe tool on the command line. For more information, see How to: Precompile ASP.NET Web Sites for Deployment and ASP.NET Compilation Tool (Aspnet_compiler.exe). Visual Studio also includes commands to precompile a Web site from the IDE.

What does Precompile during publishing do?

The default (checked) setting of "Allow precompiled site to be updateable" allows you to update your view content without needing to rebuild the entire project.

Do ASPX CS files need to be compiled?

In order for the ASP.NET engine to service a request for this page, the page's code portion (the WebPage. aspx. cs file) must first be compiled. This compilation can happen explicitly or automatically.


1 Answers

Precompile asp.net views with ms build

You should use the arguments /p:PrecompileBeforePublish=true instead of /p:MvcBuildViews=true.

MvcBuildViews is often mistakenly considered as something that when activated results in precompiled views. Actually. It is just a thing to include views to build process but it doesn’t compile these to project binaries folder.

When we check the checkbox Precompile during publish and uncheck the checkbox Allow precompiled site to be updateable on the file publish options, we will get following properties setting in our FolderProfile.pubxml file:

  <PropertyGroup>
    <PrecompileBeforePublish>True</PrecompileBeforePublish>
    <EnableUpdateable>False</EnableUpdateable>
  </PropertyGroup>

So if you want to do the same with msbuild tool, we should use the arguments:

/p:PrecompileBeforePublish=true;EnableUpdateable=false

Besides, since those arguments are stored in the .pubxml file(under the PublishProfiles in the Properties node in Solution Explorer). They are now designed to be checked in and shared with team members. These files are now MSBuild files and you can customize them if you wish. In order to publish from the command line just pass DeployOnBuild=true and set PublishProfile to the name of the profile:

msbuild.exe "TestPrecompiled.csproj" /p:DeployOnBuild=true /p:PublishProfile=FolderProfile.pubxml

Of course, you can use the arguments and the .pubxml file at the same time, the arguments in the command line will overwrite the property in the .pubxml file:

msbuild.exe "TestPrecompiled.csproj" /p:DeployOnBuild=true /p:PublishProfile=FolderProfile.pubxml /p:PrecompileBeforePublish=true;EnableUpdateable=false

After publish completed, open the .cshtml file in the publish folder, we will get the line This is a marker file generated by the precompilation tool, and should not be deleted! as they do when publishing from VS:

enter image description here

enter image description here

See Precompiling ASP.NET WebForms and MVC Views with MSBuild for some more details.

like image 189
Leo Liu-MSFT Avatar answered Sep 20 '22 15:09

Leo Liu-MSFT