【发布时间】:2015-07-09 09:59:33
【问题描述】:
我们正在使用新的 ASP .NET 5 平台构建一个 Web 应用程序。我正在配置构建和部署自动化工具,并且我希望能够在部署期间更改应用程序设置(例如更改 Web 服务 url)。在 ASP .NET 5 中,我们不再有 web.config 文件,只有新的 json 配置文件。 ASP .NET 5 中是否有类似于以前版本的 ASP .NET 中的 web.config 转换的机制?
【问题讨论】:
我们正在使用新的 ASP .NET 5 平台构建一个 Web 应用程序。我正在配置构建和部署自动化工具,并且我希望能够在部署期间更改应用程序设置(例如更改 Web 服务 url)。在 ASP .NET 5 中,我们不再有 web.config 文件,只有新的 json 配置文件。 ASP .NET 5 中是否有类似于以前版本的 ASP .NET 中的 web.config 转换的机制?
【问题讨论】:
我知道 web.configs 并不真正受支持,但它们仍在 IIS 下的 ASP.Net 中使用。
我想应用转换,也想从配置中控制环境变量,如下所示:
<aspNetCore>
<environmentVariables xdt:Transform="Replace">
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
</environmentVariables>
</aspNetCore>
如果你真的想在 ASP.Net core/5 中转换它们,你可以使用以下方法:
根据需要添加任意数量的不同 web.config 转换文件 项目。例如,可以添加 Web.Development.config, Web.Staging.config 和 Web.Production.config 等...随心所欲地命名它们。
修改您的 project.json 文件以通过添加此文件来输出文件 在当前 web.config 行下方的 publishoptions 行: "web.*.config"
创建发布配置文件并为您的 发布配置文件(位于 Web Project\Properties\PublishProperties\profilename-publish.ps1)以添加以下修改:
在try catch上面添加这个函数(我在这里Web.Config transforms outside of Microsoft MSBuild?找到了这个函数,稍作修改。):
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);
}
在 Publish-AspNet 调用ABOVE上添加这些行:
$xdtFiles = Get-ChildItem $packOutput | Where-Object {$_.Name -match "^web\..*\.config$"};
$webConfig = $packOutput + "web.config";
foreach($xdtFile in $xdtFiles) {
XmlDocTransform -xml $webConfig -xdt "$packOutput$xdtFile"
}
【讨论】:
您实际上并不需要在 ASP.NET 5 中进行配置转换,因为它对链式配置源提供了开箱即用的支持。比如取this sample:
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 文件,并且可以通过从环境变量中提供正确的配置来覆盖它。
查看the Configuration docs了解更多信息。
【讨论】:
正如@tugberk 所指出的,您可以改用环境变量,这是处理这种情况的更好方法。如果您在开发环境中运行并且想要存储密码或连接字符串,您也可以使用用户密码来添加它们。毕竟,您仍然可以像这样使用特定于环境的配置文件(这是一个 ASP.NET 5 Beta 5 示例):
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();
【讨论】: