简体   繁体   English

IIS 中的 ASP.NET Core,(ICollection/List/Array) appsettings.json -> web.config

[英]ASP.NET Core in IIS, (ICollection/List/Array) appsettings.json -> web.config

Using the Options pattern I created a class with an ICollection<string> allowedHosts property.使用选项模式,我创建了一个带有ICollection<string> allowedHosts属性的类。 Inside the appsettings.json I've added [ "google.nl", "bing.com" ] and when the app is running in Development mode everything is fine.appsettings.json我添加了[ "google.nl", "bing.com" ]并且当应用程序在开发模式下运行时一切正常。

However, in production, we use IIS and by using the Configuration Editor of IIS to define the environmentVariables , and this gets saved as web.config and will somewhat look like this:但是,在生产中,我们使用 IIS 并通过使用 IIS 的配置编辑器来定义environmentVariables ,这将保存为web.config并且看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <aspNetCore>
            <environmentVariables>
                <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
                <environmentVariable name="AllowedHosts" value="How to format?" />
            </environmentVariables>
        </aspNetCore>
    </system.webServer>
</configuration>

My problem is: how can I convert this JSON from the appsettings.json :我的问题是:如何从appsettings.json转换此 JSON:

{
  "allowedHosts": [ "google.com", "bing.com" ]
}

To XML for the web.config file?web.config文件的 XML? I tried using ["google.com"] , "google.com" but none is working.我尝试使用["google.com"]"google.com"但都没有工作。

Edit: I've used a bad property name, in this case the AllowedHosts has a different use and I should change the property name.编辑:我使用了一个错误的属性名称,在这种情况下AllowedHosts有不同的用途,我应该更改属性名称。

The AllowedHosts is a special configuration that accepts a semicolon-delimited list of host names without port numbers. AllowedHosts是一种特殊的配置,它接受以分号分隔的主机名列表,没有端口号。 For more details, see Host filtering .有关更多详细信息,请参阅主机过滤

If you need allow multiple hosts, you could configure it as below :如果您需要允许多个主机,您可以将其配置如下:

<aspNetCore processPath="dotnet" arguments=".\abc.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess">
    <environmentVariables>
        <environmentVariable name="AllowedHosts" value="google.com;bing.com" />
    </environmentVariables>
</aspNetCore>

If you would like to get array from Web.Config configuration, you could change the key name to be :如果您想从Web.Config配置中获取数组,您可以将键名更改为:

<key-name>:<index>

eg :例如:

<environmentVariables>
    <environmentVariable name="AllowedHosts:0" value="google.com" />
    <environmentVariable name="AllowedHosts:1" value="bing.com" />
</environmentVariables>

To get the array in ASP.NET Core :在 ASP.NET Core 中获取数组:

var allowed =  _config.GetSection("AllowedHosts").AsEnumerable();

/* output :
[  
   {  
      "key":"AllowedHosts",
      "value":"*"
   },
   {  
      "key":"AllowedHosts:1",
      "value":"bing.com"
   },
   {  
      "key":"AllowedHosts:0",
      "value":"google.com"
   }
]
*/

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

相关问题 使用ASP.NET Core反序列化appsettings.json - Deserialize appsettings.json with ASP.NET Core ASP.NET Core appsettings.json 更新代码 - ASP.NET Core appsettings.json update in code ASP.NET Core appsettings.json需要在磁盘上更新 - ASP.NET Core appsettings.json need to update on disk 用于 MSTest [ASP.NET Core 3.1] 的 appsettings.json - appsettings.json for MSTest [ASP.NET Core 3.1] 依赖于文件的.net核心asp.net单元测试 - appsettings.json - 在travis中失败 - .net core asp.net unit tests that depend on files - appsettings.json - fail in travis ASP.NET Core 3 - Serilog 如何在 appsettings.json 文件中配置 Serilog.Sinks.Map? - ASP.NET Core 3 - Serilog how to configure Serilog.Sinks.Map in appsettings.json file? ASP.NET Core 2.0忽略了appsettings.json(dev和prod)? - appsettings.json (both dev and prod) ignored by ASP.NET Core 2.0? ASP.net Core 2.0中的appsettings.json预览配置GetSection null - appsettings.json in ASP.net Core 2.0 Preview configuration GetSection null appsettings.json | ASP.NET 核心 MVC 3.1 | 值必须是以下类型之一:字符串 - appsettings.json | ASP.NET Core MVC 3.1 | Value must be one of the following types: string 您可以预览 ASP.NET Core 的 appsettings.json 环境覆盖吗? - Can you preview ASP.NET Core's appsettings.json environment overrides?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM