'How to deploy an ASP.NET MVC application on build using MSBuild in Visual Studio 2015?

I want the application to be deployed to a local folder when I build the project. This should produce a zip file that I can use to import in IIS. How can I achieve this?



Solution 1:[1]

Command line

msbuild yoursolutionfile.sln /p:DeployOnBuild=true 
/p:WebPublishMethod=Package 
/p:PackageAsSingleFile=true 
/p:PackageLocation="yourpath\yourfilename.zip"

DeployOnBuild tells msbuild to deploy (to package location)

WebPublishMethod Creates a deployment package. You can choose other options for web publish like file copy.

PackageAsSingleFile zips the output

Above is all ran at the command line and while not in VS to do it, does build and deploy in one step. You could have a command window open where you run this command whenever you want to.

IN VS

You can use the Web Deployment Publish Wizard in VS. It's in the Build Menu. It will walk you through the steps for this as well.

However, this is only after the build occurs and is a second step.

To do it all at once, well, web publish on build via VS is not "supported". As in, not an easy way to do it. You could run the cmd I showed in the post-build but that literally means a second recompile and I think that's a horrible idea, so don't unless this is a really small project and even then, no, ok maybe.

In VS in one step

Back to MSBUILD but with changing the project file's xml (msbuild commands).

First create your publish profile (the file created by the publish wizard). This essentially creates the settings for msbuild from my first example but saves it to a file.

Then edit your project file (csproj) as described by @richardSzalay or @chief7

The csproj file is the msbuild commands that VS uses. In those examples, you create some properties (propertygroup) to setup the deployment and an AfterBuild target to run the publish.

Solution 2:[2]

Find the below property groups from the csproj:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

First one is for debug and the second for release.

Add the following inside both property groups (If you want the auto build to happen in both debug and release mode).

<DeployOnBuild>true</DeployOnBuild>
<PublishProfile>Server</PublishProfile>

It will now auto publish for both Visual studio build and MSBuild.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2