简体   繁体   English

将Web应用程序部署到Azure时,将覆盖web.config的其他部分,而不是appSettings和connectionString

[英]Overriding other parts of the web.config rather than the appSettings and connectionString when deploying web apps to Azure

When deploying web apps to Azure you can override the connection strings and app settings by setting these in the portal for the particular web app you are deploying. 将Web应用程序部署到Azure时,可以通过在门户中为要部署的特定Web应用程序设置连接字符串和应用程序设置来覆盖它们。 Is there any way you can override other parts of the web.config from the portal. 有什么方法可以从门户覆盖web.config的其他部分。 I am using a plugin for the Umbraco CMS that requires connection strings in the web.config that are not in the appSetting section of the file. 我正在为Umbraco CMS使用插件,该插件要求web.config中的连接字符串不在文件的appSetting部分中。 (they usually hold them in another file, but I have placed them in the web.config so I can hopefully set them from the portal) (他们通常将它们保存在另一个文件中,但是我已经将它们放置在web.config中,因此希望可以从门户网站进行设置)

Here is a sample of the configuration section of the web.config with only the relevant section I am referring to: 这是web.config的配置部分的示例,其中仅包含我所指的相关部分:

<configuration>
    ...A lot of other web.config stuff
  <imageProcessor>   
    <caching currentCache="AzureBlobCache">
      <caches>
        <cache name="AzureBlobCache" type="ImageProcessor.Web.Plugins.AzureBlobCache.AzureBlobCache, ImageProcessor.Web.Plugins.AzureBlobCache" maxDays="365">
          <settings>
            <!--Azure Cache Provider-->
            <setting key="CachedStorageAccount" value="Azure blob storage key" />
            <setting key="CachedBlobContainer" value="cache" />
            <setting key="UseCachedContainerInUrl" value="false" />
            <setting key="CachedCDNRoot" value="Azure storage account" />
            <setting key="CachedCDNTimeout" value="1000" />
            <setting key="SourceStorageAccount" value="name=Azure storage account" />
            <setting key="SourceBlobContainer" value="media" />
            <setting key="StreamCachedImage" value="false" />
          </settings>
        </cache>
      </caches>
    </caching>
  </imageProcessor>
</configuration>

What I would like to do is set the setting keys in the cache section above. 我想做的是在上面的缓存部分中设置设置键。 If I am not able to refer directly to this section in the Azure portals appSettings section then can I put these settings in the appSettings section of the web.config as keys and refer to them from this section of the web.config file? 如果我不能直接在Azure门户的appSettings部分中引用此部分,那么可以将这些设置作为键放在web.config的appSettings部分中,并可以从web.config文件的此部分中引用它们吗?

I know I can use web.config transforms to achieve this but this will still require me to check the connection strings into source control under web.config.release etc and I would prefer not to do this. 我知道我可以使用web.config转换来实现此目的,但这仍然需要我将连接字符串检查到web.config.release等下的源代码控制中,我不希望这样做。

Is there another solution that I am overlooking that will perform the tasks I want? 我是否还有其他解决方案可以执行我想要的任务? (The solution is hosted in VSTS so I could always add another build step with the settings keys there) That way they would end up associated with the build configuration rather than in source control. (该解决方案托管在VSTS中,因此我总是可以在其中使用设置键添加另一个构建步骤。)这样,它们最终将与构建配置关联,而不是与源代码管理关联。

You can add PowerShell Script as one of the steps in your build definition. 您可以将PowerShell脚本添加为构建定义中的步骤之一。 If you use inline script option you don't need to have this script in your repo. 如果使用内联脚本选项,则您的存储库中不需要此脚本。

Script can look like this (I added a correct XPath as stated in your example). 脚本看起来像这样(如您的示例中所述,我添加了正确的XPath)。

$path = ".\web.config"
$xml = [xml](Get-Content $path)

$dictionary = @{
    CachedCDNTimeout = '9000'    
}

foreach ($key in $dictionary.Keys)
{    
    if (($addKey = $xml.SelectSingleNode("//configuration/imageProcessor/caching/caches/cache/settings/setting[@key = '$key']")))
    {        
        $addKey.SetAttribute('value', $dictionary[$key])
    }
}

$xml.Save($path)

(I've tested it only locally) (我只在本地测试过)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM