Skip to content

Commit 20a592b

Browse files
committed
add post about multiple configuration files
1 parent 4708f1a commit 20a592b

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<!DOCTYPE html>
2+
<html lang="en-US" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
5+
<title>Add custom configuration file in a ASP.NET Core Web Application using options and configuration objects</title>
6+
<link rel="shortcut icon" href="/wwwroot/favicon.ico" />
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
8+
<meta name="language" content="en" />
9+
<meta name="keywords" content="ASP.NET Core,ASP.NET Core web application,ASP.NET Core Configuration,appsettings.json" />
10+
<meta name="description" content="How to add a custom configuration file in a ASP.NET Core Web Application using options and configuration objects" />
11+
<meta name="author" content="Adrien Torris" />
12+
<meta name="robots" content="index,follow" />
13+
<meta name="generator" content="No404! 0.1" />
14+
<link rel="alternate" href="https://adrientorris.github.io/aspnet-core/add-custom-configuration-file-using-options-and-configuration-objects.html" hreflang="en-en" />
15+
<link href="/wwwroot/css/style.css" rel="stylesheet" />
16+
</head>
17+
<body>
18+
<div id="main_ctn" class="post_ctn">
19+
<header>
20+
<div id="header-content">
21+
<h2>Blog</h2>
22+
</div>
23+
</header>
24+
<div id="arianne_wrapper">
25+
<ul>
26+
<li><a href="https://adrientorris.github.io/index.html" title="">Home</a></li>
27+
<li>ASP.NET Core</li>
28+
<li class="active"><a href="https://adrientorris.github.io/aspnet-core/add-custom-configuration-file-using-options-and-configuration-objects.html" title="Add custom configuration file using options and configuration objects">Add custom configuration file using options and configuration objects</a></li>
29+
</ul>
30+
</div>
31+
<div id="post_ctn">
32+
<h1>Add custom configuration file using options and configuration objects</h1>
33+
<p>It can be usefull to have multiple configuration files, and it follows some develoment best practices like the Interface Segregation Principle or the Separation of Concerns. Moreover, it allows you to have the behavior you want on each configuration file.</p>
34+
<p>It's very simple and asy to do this using options and configuration objects.</p>
35+
<p>The first step is to add a file in your project, here <span class="thnclwrd">customSettings.json</span>, with the content :</p>
36+
<pre><code class="json">
37+
&#123;
38+
"CustomSection1": &#123;
39+
"Hi": "Hi!",
40+
"Hello": "Hello!"
41+
&#125;,
42+
"CustomSection2": &#123;
43+
"Bye": "Bye!"
44+
&#125;
45+
&#125;
46+
</code></pre>
47+
<p>Create the model of the configuration settings you want to add :</p>
48+
<pre><code class="csharp">
49+
namespace WebApplication1.Models.CustomSettings
50+
&#123;
51+
public class CustomSection1
52+
&#123;
53+
public string Hi { get; set; }
54+
public string Hello { get; set; }
55+
&#125;
56+
57+
public class CustomSection2
58+
{
59+
public string Bye { get; set; }
60+
}
61+
&#125;
62+
</code></pre>
63+
<p>Then, use the configuration provider you want (the <span class="thnclwrd">JSON</span> one here), and add your class to the service container and bound to configuration :</p>
64+
<pre><code class="csharp">
65+
public class Startup
66+
{
67+
public Startup(IHostingEnvironment env)
68+
{
69+
var builder = new ConfigurationBuilder()
70+
.SetBasePath(env.ContentRootPath)
71+
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
72+
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
73+
.AddJsonFile($"customSettings.json", optional: true, reloadOnChange: true)
74+
.AddEnvironmentVariables();
75+
76+
if (env.IsDevelopment())
77+
{
78+
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
79+
builder.AddApplicationInsightsSettings(developerMode: true);
80+
}
81+
Configuration = builder.Build();
82+
}
83+
84+
public IConfigurationRoot Configuration { get; }
85+
86+
// This method gets called by the runtime. Use this method to add services to the container.
87+
public void ConfigureServices(IServiceCollection services)
88+
{
89+
// Add framework services.
90+
services.AddApplicationInsightsTelemetry(Configuration);
91+
92+
services.Configure&#60;CustomSection1&#62;(options =&#62; Configuration.GetSection("CustomSection1").Bind(options));
93+
services.Configure&#60;CustomSection2&#62;(options =&#62; Configuration.GetSection("CustomSection2").Bind(options));
94+
95+
services.AddMvc();
96+
}
97+
</code></pre>
98+
<p>Work is done ! You can now access your settings from your controllers using <span class="thnclwrd">Dependency Injection</span> on <span class="thnclwrd">IOptions<TOptions></span> :</p>
99+
<pre><code class="csharp">
100+
public class HomeController : Controller
101+
{
102+
private readonly CustomSection1 _customSection1;
103+
104+
private readonly CustomSection2 _customSection2;
105+
106+
public HomeController(
107+
IOptions&#60;CustomSection1&#62; customSection1
108+
, IOptions&#60;CustomSection2&#62; customSection2)
109+
{
110+
_customSection1 = customSection1.Value;
111+
_customSection2 = customSection2.Value;
112+
&#125;
113+
</code></pre>
114+
<div id="crdny">December 27, 2016</div>
115+
<div id="tags_wrapper">
116+
<ul>
117+
<li>ASP.NET Core</li>
118+
<li>ASP.NET Core Configuration</li>
119+
</ul>
120+
</div>
121+
<div id="refs_wrapper">
122+
<ul>
123+
<li><a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration" title="Official documentation about Configuration" target="_blank">Configuration Docs</a></li>
124+
</ul>
125+
</div>
126+
</div>
127+
<footer>
128+
</footer>
129+
</div>
130+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
131+
<script src="/wwwroot/js/Infra.js" type="text/javascript"></script>
132+
<link type="text/css" rel="stylesheet" href="/wwwroot/lib/highlight/styles/vs.css" />
133+
<script type="text/javascript" src="/wwwroot/lib/highlight/highlight.pack.js"></script>
134+
<script type="text/javascript">hljs.initHighlightingOnLoad();</script>
135+
<script>
136+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
137+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
138+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
139+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
140+
141+
ga('create', 'UA-85948839-1', 'auto');
142+
ga('send', 'pageview');
143+
144+
</script>
145+
</body>
146+
</html>

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ <h2>Blog</h2>
2727
</div>
2828
</header>
2929
<div id="post_listing">
30+
<div class="post_listing_e">
31+
<i>December 27, 2016</i>
32+
<br />
33+
<a href="https://adrientorris.github.io/aspnet-core/add-custom-configuration-file-using-options-and-configuration-objects.html" title="Add custom configuration file using options and configuration objects with ASP.NET Core">Add custom configuration file using options and configuration objects</a>
34+
</div>
3035
<div class="post_listing_e">
3136
<i>16/12/2016</i>
3237
<br />

0 commit comments

Comments
 (0)