2

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"));
2
  • What do you mean by putting the cross settings in appsetting? Commented May 23, 2020 at 13:12
  • I can change the settings in the appsettings.json file. Commented May 23, 2020 at 13:16

1 Answer 1

7

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.