You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<liclass="active"><ahref="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
+
<divid="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 <spanclass="thnclwrd">customSettings.json</span>, with the content :</p>
36
+
<pre><codeclass="json">
37
+
{
38
+
"CustomSection1": {
39
+
"Hi": "Hi!",
40
+
"Hello": "Hello!"
41
+
},
42
+
"CustomSection2": {
43
+
"Bye": "Bye!"
44
+
}
45
+
}
46
+
</code></pre>
47
+
<p>Create the model of the configuration settings you want to add :</p>
48
+
<pre><codeclass="csharp">
49
+
namespace WebApplication1.Models.CustomSettings
50
+
{
51
+
public class CustomSection1
52
+
{
53
+
public string Hi { get; set; }
54
+
public string Hello { get; set; }
55
+
}
56
+
57
+
public class CustomSection2
58
+
{
59
+
public string Bye { get; set; }
60
+
}
61
+
}
62
+
</code></pre>
63
+
<p>Then, use the configuration provider you want (the <spanclass="thnclwrd">JSON</span> one here), and add your class to the service container and bound to configuration :</p>
<p>Work is done ! You can now access your settings from your controllers using <spanclass="thnclwrd">Dependency Injection</span> on <spanclass="thnclwrd">IOptions<TOptions></span> :</p>
Copy file name to clipboardExpand all lines: index.html
+5Lines changed: 5 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,11 @@ <h2>Blog</h2>
27
27
</div>
28
28
</header>
29
29
<divid="post_listing">
30
+
<divclass="post_listing_e">
31
+
<i>December 27, 2016</i>
32
+
<br/>
33
+
<ahref="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>
0 commit comments