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

梦想的初衷 提交于 2019-12-23 05:42:25

问题


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?


回答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.




回答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>EMRFHIRServer</PublishProfile>

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



来源:https://stackoverflow.com/questions/53595614/how-to-deploy-an-asp-net-mvc-application-on-build-using-msbuild-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!