Reputation: 420
I'm using a webapi .netcore project. I want to put all the cross settings in the appsettings.json file. How do I do this?
This is my code:
app.UseCors(x => x.WithOrigins("http://localhost:4200")
.AllowCredentials()
.WithHeaders("content-type")
.WithMethods("GET", "POST", "PUT", "DELETE"));
Upvotes: 2
Views: 4436
Reputation: 18139
If you want to set the CORS settings in appsettings.json
and use the settings in startup.cs
, you can follow the code below:
This is my appsettings.json
:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"AllowedOrigins": "http://localhost:4200",
"AllowedHeaders": "content-type",
"AllowedMethods": "GET,POST,PUT,DELETE"
}
This is my partial code in startup.cs
:
app.UseCors(x => x.WithOrigins(Configuration.GetSection("AllowedOrigins").Value.Split(","))
.AllowCredentials().WithHeaders(Configuration.GetSection("AllowedHeaders").Value.Split(","))
.WithMethods(Configuration.GetSection("AllowedMethods").Value.Split(",")));
app.UseHttpsRedirection();
Upvotes: 7