[英]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"
}
]
*/
Wouldn't you set that up in your Startup class?你不会在你的 Startup 类中设置它吗? https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.2 https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.