How to specify a custom parameters.xml when building a Web Deploy package for ASP.NET Core?

Josh Schultz picture Josh Schultz · Oct 16, 2017 · Viewed 8.9k times · Source

Overview

I am building a deployable web package that can be imported into IIS that automatically prompts for settings needed by my ASP.NET Core application. I already created a package that will deploy just fine, except after deploying, I need to manually find/edit my appsettings.json file.

I know this package can include a parameters.xml file that will automatically prompt and fill in my appsettings.json when importing an app into IIS. I have already made a parameters.xml file, and manually added it to my package after building it; it worked as expected. I'd just like to have msbuild automatically add the parameters.xml file to the package for me.

A separate project of mine (ASP.NET MVC 4) already does this. For that, I simply needed to put my parameters.xml in the same folder as my .csproj. I tried doing the same here, but had no luck.

Repro Steps

I created an ASP.NET Core Web Application ASP.NET Core Web Application Using .NET Framework on ASP.NET Core 1.1 .NET Framework with ASP.NET Core 1.1 I then went to publish my website

Publish dropdown

Selected 'Folder' (just to get a template) Folder Publish

I then edited the profile and changed the WebPublishMethod to Package and added the three lines below it.

<DesktopBuildPackageLocation>bin\$(Configuration)\$(MSBuildProjectName).zip</DesktopBuildPackageLocation>
<PackageAsSingleFile>true</PackageAsSingleFile>
<DeployIisAppPath>External</DeployIisAppPath>

Modified publish profile

I then published one more time. Now I get a WebDeploy package that I can deploy to IIS.

Great! but...

I'd like to customize the parameters.xml.

Parameters.xml I'd like to customize

For previous projects, I was able to add a parameters.xml file to my project root, and VS/msbuild would automatically add it to my published package. This currently works for a different project using ASP.NET MVC 4.

So, I tried the same thing for this project. First I added a settings.json with a very simple setting:

{
  "SettingName": ""
}

Then I added a parameters.xml file that I know works to my project root. (If I manually replace the parameters.xml file in Sample.zip package, it correctly prompts and replaces my setting when deploying)

<parameters>
  <parameter name="IIS Web Application Name" value="External" tags="IisApp">
    <parameterEntry kind="ProviderPath" scope="IisApp" match="^c:\\users\\joshs\\documents\\visual\ studio\ 2017\\Projects\\Sample\\Sample\\obj\\Release\\net461\\win7-x86\\PubTmp\\Out\\$" />
  </parameter>
  <parameter name="Setting Name" description="Enter a custom app setting" defaultValue="Default Setting Value">
    <parameterEntry kind="TextFile" scope="obj\\Debug\\net461\\win7-x86\\PubTmp\\Out\\appsettings\.json$" match="(?&lt;=\&quot;SettingName\&quot;\s*:\s*\&quot;)[^\&quot;]*" />
  </parameter>
</parameters>

Again, I right click and Publish once more. This time with the parameters.xml file.

Project structure with Publish menu shown

I expect the Sample.zip to contain the parameters.xml that I added to my project root, but it does not. It is the exact same as from my original publish.

Question

During the build process when creating a web deploy package, how do you include custom settings in the parameters.xml?

I have already tried this...

I already looked at https://stackoverflow.com/a/46338042/2494785, but with no luck, though my command differed slightly from the original poster.

PS C:\Users\joshs\Documents\Visual Studio 2017\Projects\Sample> & 'C:\Program Files (x86)\Microsoft Visual Studio\2017\E
nterprise\MSBuild\15.0\Bin\MSBuild.exe' .\Sample.sln /t:Sample /p:DeployOnBuild=true /p:PublishProfile=FolderProfile /p:
ProjectParametersXMLFile="C:\Temp\parameters.xml"

Answer

Josh Schultz picture Josh Schultz · May 2, 2018

I was able to solve this from peteawood's comment from an issue posted on GitHub.

https://github.com/aspnet/websdk/issues/201#issuecomment-349990389

In ASP.NET Core 2.0+ you can add the following to your .csproj

<Project Sdk="Microsoft.NET.Sdk.Web">
    .
    .
    <Target Name="AddMoreParameters" AfterTargets="_CreateParameterFiles">
        <Copy SourceFiles="Parameters.xml" DestinationFiles="$(_MSDeployParametersFilePath)" />
    </Target>
</Project>

SourceFiles should point to the location of your parameters.xml file from the perspective of the .csproj file. My parameters.xml is found in the same directory as my project file.