Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration file transformation in ASP .NET 5

We are building a web-application using the new ASP .NET 5 platform. I am configuring the build and deployment automation tools and I want to have the ability to change the application settings during deployment (like changing the web-service url). In ASP .NET 5 we don't have web.config files anymore, only the new json configuration files. Is there a mechanism in ASP .NET 5 similar to web.config transformation in the previous versions of ASP .NET?

like image 543
Olga Zemskova Avatar asked Mar 16 '23 11:03

Olga Zemskova


1 Answers

I know that web.configs are not really supported, but they are still used in ASP.Net under IIS.

I had a desire to apply transforms as well as I wanted to control the environment variable from the config like so:

<aspNetCore>
  <environmentVariables xdt:Transform="Replace">
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
  </environmentVariables>
</aspNetCore>

If you really want to transform them in ASP.Net core / 5 you can use the following method:

  1. Add as many different web.config transform files as you want to your project. For example, you can add Web.Development.config, Web.Staging.config, and Web.Production.config, etc... Name them however you like.

  2. Modify your project.json file to output the files by adding this line to the publishoptions right below your current web.config line: "web.*.config"

  3. Create a publish profile and modify your powershell script for your publish profile (located at Web Project\Properties\PublishProperties\profilename-publish.ps1) to add the below modifications:

Add this function above the try catch (I found this function here Web.Config transforms outside of Microsoft MSBuild?, slightly modified.) :

function XmlDocTransform($xml, $xdt)
{
    if (!$xml -or !(Test-Path -path $xml -PathType Leaf)) {
        throw "File not found. $xml";
    }
    if (!$xdt -or !(Test-Path -path $xdt -PathType Leaf)) {
        throw "File not found. $xdt";
    }

    "Transforming $xml using $xdt";

    $scriptPath = (Get-Variable MyInvocation -Scope 1).Value.InvocationName | split-path -parent
    #C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.Tasks.dll

    Add-Type -LiteralPath "${Env:ProgramFiles(x86)}\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.XmlTransform.dll"

    $xmldoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
    $xmldoc.PreserveWhitespace = $true
    $xmldoc.Load($xml);

    $transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt);
    if ($transf.Apply($xmldoc) -eq $false)
    {
        throw "Transformation failed."
    }
    $xmldoc.Save($xml);
}

Add these lines ABOVE the Publish-AspNet call:

$xdtFiles = Get-ChildItem $packOutput | Where-Object {$_.Name -match "^web\..*\.config$"};
$webConfig = $packOutput + "web.config";
foreach($xdtFile in $xdtFiles) {

    XmlDocTransform -xml $webConfig -xdt "$packOutput$xdtFile"
}
like image 75
SpaceGhost440 Avatar answered Apr 26 '23 12:04

SpaceGhost440