title: Get built settings options in Program.cs for .net Web api
body: <p>So for ease of use I decided to map appsettings.json to settings classes. I have for example one section in appsettings:</p>
  <pre><code>  &quot;Web&quot;: {
      &quot;EnableCors&quot;: true,
      &quot;CorsOrigins&quot;: [
        &quot;http://localhost:3000&quot;,
        &quot;http://localhost:44315&quot;
      ] 
  </code></pre>
  <p>With a class that this should map to</p>
  <pre><code> public class WebSettings
      {
          public static string SectionName = &quot;Web&quot;;
          public bool EnableCors { get; set; }
  
          public List&lt;string&gt; CorsOrigins { get; set; }
      }
  </code></pre>
  <p>And in Program.cs I have added:</p>
  <pre><code>builder.Services.Configure&lt;WebSettings&gt;(
      builder.Configuration.GetSection(WebSettings.SectionName));
  </code></pre>
  <p>This works great for any controller where I only have to add</p>
  <p>IOptions webSettings</p>
  <p>To the constructor for.</p>
  <p>However, to the question. Is it possible to use this binding already in program.cs. So for websettings I want to, if &quot;EnableCors&quot; is true add the list of CorsOrigins</p>
  <pre><code>// Here I want to preferably use the IOption&lt;WebSettings&gt; object.
  // Especially so I get the lost of Cors adresses to loop through and add.
  if (configuration[&quot;Web:EnableCors&quot;] == &quot;true&quot;)
  {
  
      builder.Services.AddCors(options =&gt;
      {
          options.AddPolicy(MyAllowSpecificOrigins,
              b =&gt;
              {
                  //configuration[&quot;Web:CorsOrigin&quot;]
                  b.WithOrigins(&quot;http://localhost:3000&quot;);
                  b.WithOrigins(&quot;http://localhost:44315&quot;);
                  b.AllowAnyHeader();
                  b.AllowAnyMethod();
                  b.AllowAnyOrigin();
              });
      });
  
  }
  </code></pre>
  <p>Is that even possible/viable/good solution?</p>