{"id":8391,"date":"2020-07-11T16:09:17","date_gmt":"2020-07-11T16:09:17","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8391"},"modified":"2020-07-11T16:09:17","modified_gmt":"2020-07-11T16:09:17","slug":"configuration-and-blazor","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/configuration-and-blazor\/","title":{"rendered":"Configuration and Blazor"},"content":{"rendered":"<p>In a previous post we looked at dependency injection within Blazor. One of the services available by default is an implementation of IConfiguration, hence to inject the configuration object we do the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@inject IConfiguration Configuration\r\n<\/pre>\n<p>and we can interact with the configuration using a key, i.e.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@Configuration&#x5B;&quot;SomeKey&quot;]\r\n<\/pre>\n<p>For Blazor on the server we can simply add the key\/value to the appsettings.json file, like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&quot;SomeKey&quot;:  &quot;Hello World&quot; \r\n<\/pre>\n<p>By default Blazor WebAssembly does not come with an appsettings.json, so you&#8217;ll need to add one yourself. This file will need to be in the wwwroot folder as these files will be deployed at part of your client.<\/p>\n<p>You can put sensitive data in the server appsettings.json because it&#8217;s hosted on the server, do <strong>not put sensitive information in the appsettings.json for a WebAssembly\/client<\/strong> otherwise this will be downloaded to the user&#8217;s machine.<\/p>\n<p>If you want to store more complicated data in the appsettings.json, for example a JSON object, you&#8217;ll need to create a class that looks like the data, for example if your appsettings.json had the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&quot;Person&quot;: {\r\n  &quot;FirstName&quot;: &quot;Scooby&quot;,\r\n  &quot;LastName&quot;: &quot;Doo&quot;\r\n} \r\n<\/pre>\n<p>So now we&#8217;ll need a class to match the structure of the data. The name of the class is not important, the structure is, so here we have<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyPerson\r\n{\r\n   public string FirstName { get; set; }\r\n   public string LastName { get; set; }\r\n}\r\n<\/pre>\n<p>Now to access this we need to use <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nConfiguration.GetSection(&quot;Person&quot;).Get&lt;MyPerson&gt;();\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=hIfjjOsD4ic\" rel=\"noopener noreferrer\" target=\"_blank\">Carl Franklin&#8217;s Blazor Train: Configuration and Dependency Injection<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post we looked at dependency injection within Blazor. One of the services available by default is an implementation of IConfiguration, hence to inject the configuration object we do the following @inject IConfiguration Configuration and we can interact with the configuration using a key, i.e. @Configuration&#x5B;&quot;SomeKey&quot;] For Blazor on the server we can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[305],"tags":[],"class_list":["post-8391","post","type-post","status-publish","format-standard","hentry","category-blazor"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8391"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8391\/revisions"}],"predecessor-version":[{"id":8420,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8391\/revisions\/8420"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}