-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasp.net-core-configuration.html
229 lines (197 loc) · 12.1 KB
/
asp.net-core-configuration.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<title>Warren Bates - ASP.NET Core Configuration</title>
<meta name="description" content="Warren Bates" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link type="application/rss+xml" rel="alternate" title="Warren Bates" href="/feed.rss" />
<link type="application/atom+xml" rel="alternate" title="Warren Bates" href="/feed.atom" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link href="/assets/css/bootstrap.min.css" rel="stylesheet" />
<link href="/assets/css/highlight.css" rel="stylesheet">
<link href="/assets/css/clean-blog.css" rel="stylesheet" />
<link href="/assets/css/master.css" rel="stylesheet" />
<link href="/assets/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href='//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<link href="/assets/css/override.css" rel="stylesheet" />
<meta name="application-name" content="Warren Bates" />
<meta name="msapplication-tooltip" content="Warren Bates" />
<meta name="msapplication-starturl" content="/" />
<meta property="og:title" content="Warren Bates - ASP.NET Core Configuration" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://wbates.net/posts/asp.net-core-configuration" />
<!-- TODO: More social graph meta tags -->
<script src="/assets/js/jquery.min.js"></script>
<script src="/assets/js/bootstrap.min.js"></script>
<script src="/assets/js/highlight.pack.js"></script>
<script src="/assets/js/clean-blog.js"></script>
<script src="/assets/js/d3.v3.min.js"></script>
<script src="/assets/js/trianglify.min.js"></script>
<script src="/assets/js/Please-compressed.js"></script>
<script src="/assets/js/background-check.min.js"></script>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="/assets/js/html5shiv.js"></script>
<script src="/assets/js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Warren Bates</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="/posts">Archive</a></li>
<li><a href="/tags">Tags</a></li>
<li><a href="/about">About Me</a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Header -->
<header class="intro-header" id="intro-header">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="post-heading">
<h1>ASP.NET Core Configuration</h1>
<div class="meta">
Published on 24 April 2016<br> </div>
<div class="tags">
<a role="button" href="/tags/ASPNET" class="btn btn-default btn-xs">ASP.NET</a>
<a role="button" href="/tags/ASPNET-Core" class="btn btn-default btn-xs">ASP.NET Core</a>
<a role="button" href="/tags/C%23" class="btn btn-default btn-xs">C#</a>
<a role="button" href="/tags/CSharp" class="btn btn-default btn-xs">CSharp</a>
</div>
</div>
</div>
</div>
</div>
</header>
<!-- Main Content -->
<div class="container">
<div class="row">
<div id="content" class="col-md-12">
<p>Only just beginning to dive into ASP.NET Core (or ASP.NET 5 if you prefer), but there's a great deal to like. Configuration in Core is extremely simple. Values can simply be added to a json (or other format, but why bother) configuration file and are then available to your application where Configuration is instantiated.
The default projects will include the following lines in the Startup method of Startup.cs.</p>
<pre><code class="language-csharp">var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
Configuration = builder.Build().ReloadOnChanged("appsettings.json");
</code></pre>
<p>This will load the values from the appsettings.json file (if it exists) into the Configuration property. Multiple calls to this method can be chained together (see environment specific config below). If appsettings.json looked like this</p>
<pre><code class="language-json">{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
}
},
"MyConfigValue": "An important configuration string"
}
</code></pre>
<p>The logging stuff is default, I've added the MyConfigValue key, which means that once Configuration is instantiated I can then access it's value within the Startup class with the following:</p>
<pre><code class="language-csharp">Configuration["MyConfigValue"];
</code></pre>
<p>The Logging.LogLevel.Default value can be retrieved using</p>
<pre><code class="language-csharp">Configuration["Logging:LogLevel:Default"];
</code></pre>
<p>By creating config classes specific to different areas of your application you can use the built in DI to inject that config only where it's needed.</p>
<p>If we replace MyConfigValue in the appsettings.json file above with the following key</p>
<pre><code class="language-json">"MyConfig": {
"Value1": "This is the first config value",
"Value2": "And this is the second",
"Number1": 15
}
</code></pre>
<p>Now add a new class to the project which can be called anything but I'm going with MyConfig.cs</p>
<pre><code class="language-csharp">public class MyConfig
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public int Number1 { get; set; }
}
</code></pre>
<p>As you can see the properties match up. We now need to configure the options to be populated into a MyConfig object. Back to startup.cs and find the ConfigureServices method. Add the following lines</p>
<pre><code class="language-csharp">services.AddOptions();
services.Configure<MyConfig>(Configuration.GetSection("MyConfig");
</code></pre>
<p>This takes in the Configuration property and configures it so that when we ask for a MyConfig object it will provide it using whatever it has read from appsettings.json file by matching the classes properties to the keys with the same name. To see that in use create a new controller and add a constructor passing in IOptions as a parameter. This will provide an accessor to a populated config object. which is then accessible in the constructor and can be assigned as a property on that controller.</p>
<pre><code class="language-csharp">public MyController(IOptions<MyConfig> myConfig)
{
_myConfig = myConfig.Value;
}
</code></pre>
<p>The _myConfig object will now contain our config for this section.</p>
<h1 id="environment-specific-config">Environment specific config</h1>
<p>You can easily add additional config files for each environment by calling AddJsonFile on the ConfigurationBuilder multiple times. Since I wrote this the default configure method has the following code.</p>
<pre><code class="language-csharp">var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
</code></pre>
<p>The later files take priority over the earlier ones, overwriting any earlier values. See this page for info on how to set the EnvironmentName. I also recommend adding *.development.json to your .gitignore files so that your development environment configuration isn't committed.</p>
</div>
</div>
</div>
<hr>
<!-- Footer -->
<footer>
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<p class="copyright text-muted">
Copyright © 2018
<br />
<a href="/feed.rss"><i class="fa fa-rss"></i> RSS Feed</a>
|
<a href="/feed.atom"><i class="fa fa-rss"></i> Atom Feed</a>
<br />
<strong><a href="https://wyam.io">Generated by Wyam</a></strong>
</p>
</div>
</div>
</div>
</footer>
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
// Header background
var colors = Please.make_color({
colors_returned: 3,
saturation: .6
});
var t = new Trianglify({
x_gradient: colors,
y_gradient: ["#FFFFFF"]
});
var header = document.getElementById("intro-header");
var pattern = t.generate(header.clientWidth, header.clientHeight);
header.setAttribute('style', 'background-image: ' + pattern.dataUrl);
</script>
<script>
BackgroundCheck.init({
targets: '.intro-header,.navbar',
images: '.intro-header'
});
</script>
</body>
</html>