首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP .NET 5中的配置文件转换

ASP .NET 5中的配置文件转换
EN

Stack Overflow用户
提问于 2015-07-09 09:59:33
回答 3查看 2.9K关注 0票数 4

我们正在使用新的ASP .NET 5平台构建一个web应用程序.我正在配置构建和部署自动化工具,我希望能够在部署期间更改应用程序设置(比如更改web服务url)。在ASP .NET 5中,我们不再有web.config文件,只有新的json配置文件。ASP .NET 5中是否存在类似于以前版本的ASP .NET中的web.config转换的机制?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-09 10:55:34

您实际上不需要在ASP.NET 5中进行配置转换,因为它对链式配置源有开箱即用的支持。例如,以这个样本为例

代码语言:javascript
复制
public class Startup
{
    private readonly IConfiguration _configuration;

    public Startup(IApplicationEnvironment appEnv, IHostingEnvironment env)
    {
        _configuration = new ConfigurationBuilder(appEnv.ApplicationBasePath)
            .AddJsonFile("config.json")
            .AddEnvironmentVariables()
            .Build();
    }

    // ...
}

我们添加两个配置源并构建其中的配置。如果我要求一个配置键,它将试图通过查看从上到第一顺序的源来获得该键的值。在上述情况下,我可以在开发过程中使用config.json文件,并且可以通过从环境变量中提供适当的配置来实现这一点。

有关更多信息,请访问配置文档

票数 3
EN

Stack Overflow用户

发布于 2016-09-15 14:22:34

我知道web.configs并没有得到真正的支持,但是它们仍然在IIS下的ASP.Net中使用。

我希望应用转换,并希望从配置中控制环境变量,如下所示:

代码语言:javascript
复制
<aspNetCore>
  <environmentVariables xdt:Transform="Replace">
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
  </environmentVariables>
</aspNetCore>

如果您真的想在ASP.Net核心/5中转换它们,可以使用以下方法:

  1. 向项目中添加任意多个不同的web.config转换文件。例如,您可以添加Web.Development.config、Web.Staging.config和Web.Production.config等.你想说什么就说什么。
  2. 修改project.json文件以输出文件,方法是将这一行添加到当前web.config行下方的发布页:"web.*.config“
  3. 创建发布配置文件并修改发布配置文件的powershell脚本(位于Project\Properties\PublishProperties\profilename-publish.ps1)中以添加以下修改:

将此函数添加到try (我在这里发现这个函数是Web.Config在微软MSBuild之外进行转换?,稍微修改了一下):

代码语言:javascript
复制
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);
}

之上添加以下行发布-AspNet调用:

代码语言:javascript
复制
$xdtFiles = Get-ChildItem $packOutput | Where-Object {$_.Name -match "^web\..*\.config$"};
$webConfig = $packOutput + "web.config";
foreach($xdtFile in $xdtFiles) {

    XmlDocTransform -xml $webConfig -xdt "$packOutput$xdtFile"
}
票数 4
EN

Stack Overflow用户

发布于 2015-07-10 10:45:19

正如@tugberk所指出的,您可以使用环境变量,这是处理这种情况的一种更好的方法。如果您正在开发环境中运行,并且希望存储密码或连接字符串,您也可以使用用户机密来添加它们。毕竟,您还可以像这样使用特定于环境的配置文件(这是一个ASP.NET 5 Beta 5示例):

代码语言:javascript
复制
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(
    applicationEnvironment.ApplicationBasePath);

// Add configuration from the config.json file.
configurationBuilder.AddJsonFile("config.json");

// Add configuration from an optional config.development.json, config.staging.json or 
// config.production.json file, depending on the environment. These settings override the ones in the 
// config.json file.
configurationBuilder.AddJsonFile($"config.{hostingEnvironment.EnvironmentName}.json", optional: true);

if (hostingEnvironment.IsEnvironment(EnvironmentName.Development))
{
    // This reads the configuration keys from the secret store. This allows you to store connection strings
    // and other sensitive settings on your development environment, so you don't have to check them into
    // your source control provider. See http://go.microsoft.com/fwlink/?LinkID=532709 and
    // http://docs.asp.net/en/latest/security/app-secrets.html
    configurationBuilder.AddUserSecrets();
}

// Add configuration specific to the Development, Staging or Production environments. This config can 
// be stored on the machine being deployed to or if you are using Azure, in the cloud. These settings 
// override the ones in all of the above config files.
// Note: To set environment variables for debugging navigate to:
// Project Properties -> Debug Tab -> Environment Variables
// Note: To get environment variables for the machine use the following command in PowerShell:
// $env:[VARIABLE_NAME]
// Note: To set environment variables for the machine use the following command in PowerShell:
// $env:[VARIABLE_NAME]="[VARIABLE_VALUE]"
// Note: Environment variables use a colon separator e.g. You can override the site title by creating a 
// variable named AppSettings:SiteTitle. See 
// http://docs.asp.net/en/latest/security/app-secrets.html
configurationBuilder.AddEnvironmentVariables();

IConfiguration configuration = configurationBuilder.Build();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31314201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档