{"id":6615,"date":"2016-09-21T15:41:00","date_gmt":"2016-09-21T22:41:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/dotnet\/?p=6615"},"modified":"2021-09-30T11:50:56","modified_gmt":"2021-09-30T18:50:56","slug":"reusing-configuration-files-in-asp-net-core","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/reusing-configuration-files-in-asp-net-core\/","title":{"rendered":"Reusing Configuration Files in ASP.NET Core"},"content":{"rendered":"<h2 id=\"introduction\">Introduction<\/h2>\n<p>The release of <a href=\"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2016\/06\/27\/announcing-net-core-1-0\/\">ASP.NET Core 1.0<\/a> has enticed existing ASP.NET customers to migrate their projects to this new platform. While working with a customer to move their assets to ASP.NET Core, we encountered an obstacle. Our customer had a many configuration files (<code>*.config<\/code>) they used regularly. They had hundreds of configuration files that would take time to transform into a format that could be consumed by the existing configuration providers. In this post, we&#8217;ll show how to tackle this hurdle by utilizing ASP.NET Core&#8217;s configuration extensibility model. We will write our own configuration provider to reuse the existing <code>*.config<\/code> files.<\/p>\n<p>The new <a href=\"https:\/\/docs.asp.net\/en\/latest\/intro.html#configuration\">configuration model<\/a> handles configuration values as a series of name-value pairs. There are built-in configuration providers to parse (XML, JSON, INI) files. It also enables developers to create <a href=\"http:\/\/docs.asp.net\/en\/latest\/fundamentals\/configuration.html#writing-custom-providers\">their own providers<\/a> if the current providers do not suit their needs.<\/p>\n<h2 id=\"creating-the-configurationprovider\">Creating the ConfigurationProvider<\/h2>\n<p>By following the documentation outlined in <a href=\"https:\/\/docs.asp.net\/en\/latest\/fundamentals\/configuration.html#writing-custom-providers\">Writing custom providers<\/a>, we created a class that inherited from <code>ConfigurationProvider<\/code>. Then, it was a matter of overriding the <code>public override void Load()<\/code> function to parse the data we wanted from the configuration files! Here&#8217;s a little snippet of that code below.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/conniey\/e4c50d19c97f5445657fb0f2108af9e1.js?file=ConfigurationProvider.cs\"><\/script><\/p>\n<h2 id=\"the-configurationprovider-in-action\">The ConfigurationProvider in Action<\/h2>\n<p>Consider the following <a href=\"http:\/\/www.asp.net\/mvc\/overview\/getting-started\/introduction\/creating-a-connection-string\">Web.config<\/a>:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/conniey\/faf38c3d22e24d2e75de2fb858a9dbdf.js?file=app.config\"><\/script><\/p>\n<p>to reuse this file, all we have to do is use our new provider like below and run <code>dotnet run<\/code> to see the code in action.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/conniey\/89499d47c6c4977c23254770017507a5.js?file=Program.cs\"><\/script><\/p>\n<p><a href=\"http:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2016\/09\/console.output8.png\"><img decoding=\"async\" style=\"border: 0px currentcolor;\" title=\"&#96;dotnet run&#96; console output\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2016\/09\/console.output8_thumb.png\" alt=\"&#96;dotnet run&#96; console output\" width=\"800\" height=\"393\" border=\"0\" \/><\/a><\/p>\n<p>Our configuration provider code is on <a href=\"https:\/\/github.com\/aspnet\/Entropy\/tree\/dev\/samples\/Config.CustomConfigurationProviders.Sample\">GitHub<\/a> so you can check it out yourself and see how easy it is to use the ASP.NET configuration model.<\/p>\n<h1 id=\"references\">References<\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.asp.net\/en\/latest\/conceptual-overview\/aspnet.html#why-build-asp-net-5\">Why build for ASP.NET Core?<\/a><\/li>\n<li><a href=\"https:\/\/docs.asp.net\/en\/latest\/intro.html#configuration\">ASP.NET Documentation &#8211; Configuration<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/aspnet\/Entropy\/tree\/dev\/samples\/Config.CustomConfigurationProviders.Sample\">GitHub &#8211; ConfigurationProvider Code<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The release of ASP.NET Core 1.0 has enticed existing ASP.NET customers to migrate their projects to this new platform. While working with a customer to move their assets to ASP.NET Core, we encountered an obstacle. Our customer had a many configuration files (*.config) they used regularly. They had hundreds of configuration files that would [&hellip;]<\/p>\n","protected":false},"author":36287,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685],"tags":[9,31,56],"class_list":["post-6615","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","tag-net-core","tag-asp-net","tag-configuration"],"acf":[],"blog_post_summary":"<p>Introduction The release of ASP.NET Core 1.0 has enticed existing ASP.NET customers to migrate their projects to this new platform. While working with a customer to move their assets to ASP.NET Core, we encountered an obstacle. Our customer had a many configuration files (*.config) they used regularly. They had hundreds of configuration files that would [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/6615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/36287"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=6615"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/6615\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=6615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=6615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=6615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}