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

2

12

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://mcmap.net/q/1011359/-web-deploy-package-not-picking-up-parameters-xml-when-using-latest-csproj-format, 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"
Ultramontane answered 16/10, 2017 at 19:28 Comment(0)
U
5

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.

Ultramontane answered 2/5, 2018 at 18:25 Comment(2)
Unfortunatelly this solution removes default parameter IIS Web Application Name which is required for IIS deployment. Based on the linked issue, it should be fixed in SDK 2.2.Edaphic
@JanZahradník do you have a link to the issue that you can share?Cloistral
C
3

I believe I can just pass parameters via cmd-line as properties for msbuild. It's not fully what you asked for I understand.
For example, in the following command I'm passing DeployIisAppPath property:

dotnet publish /p:WebPublishMethod=Package /p:DeployIisAppPath=mysite/myapp /p:PublishProfile=rnddev03-core-dev 

and in the output folder we'll get xxx.SetParameters.xml file with:

<parameters>
  <setParameter name="IIS Web Application Name" value="mysite/myapp" />
</parameters>
Coadjutress answered 16/10, 2017 at 23:12 Comment(2)
You're correct; you can pass properties directly to msbuild. Executing a command line statement is fine by me, though I'm not following how that would help in this case. Would you elaborate a bit, please?Ultramontane
@JoshSchultz I'm sorry, probably I not enough carefully read you question :(, you're talking about custom parameters, I doubt it's possible to configure them via msbuild properties directly.Coadjutress

© 2022 - 2024 — McMap. All rights reserved.