question

TonyWei-3308 avatar image
0 Votes"
TonyWei-3308 asked ZhiLv-MSFT commented

The code behavior of configuration has changed when upgrade from .NET Core 2.x to .NET Core 3.1

If there is a config section like below in AppSetting.json and the APP runtime is .NET Core 2.x, no exception occurred and the value of AllowedHosts should be the last one, but when I upgrade to .NET Core 3.1, the exception occurred.

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"AllowedHosts": "10.0.1.2",
"test": {
"dd": 12
}
}

144008-screenshot-2021-10-27-112037.png



I have checked he document from Microsoft site, per the document below, the code behavior should be same between .NET Core 2.x and .NET Core 3.1

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#configuration-keys-and-values-1

Configuration keys and values
Configuration keys:

Are case-insensitive. For example, ConnectionString and connectionstring are treated as equivalent keys.
If a key and value is set in more than one configuration providers, the value from the last provider added is used. For more information, see Default configuration.
Hierarchical keys
Within the Configuration API, a colon separator (:) works on all platforms.
In environment variables, a colon separator may not work on all platforms. A double underscore, __, is supported by all platforms and is automatically converted into a colon :.
In Azure Key Vault, hierarchical keys use -- as a separator. The Azure Key Vault configuration provider automatically replaces -- with a : when the secrets are loaded into the app's configuration.
The ConfigurationBinder supports binding arrays to objects using array indices in configuration keys. Array binding is described in the Bind an array to a class section.
Configuration values:

Are strings.
Null values can't be stored in configuration or bound to objects.

dotnet-csharpdotnet-aspnet-core-generaldotnet-runtime
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Probably the validation was improved. Now it does not accept invalid multiple AllowedHosts elements. If you want to enumerate several hosts, you can do something like this: "AllowedHosts": "10.0.1.2;10.0.1.3;10.0.1.7". Check if this configuration value is still used in newer .NET 5 and 6.

0 Votes 0 ·

Hi @TonyWei-3308,

This issue seems is a known issue( see this thread), and it has been updated from Asp.net core 3.X version. To set multiple host names, you could use the following code:

 {
   "AllowedHosts": "example.com;localhost"
 } 
0 Votes 0 ·

0 Answers