Tips for. NET Core 2.0 migration: Examples of the web. config configuration file, coreweb. config
Preface
I believe everyone should know that. NET Core no longer supports the original web. config configuration file, instead of the json or xml configuration file. The officially recommended project configuration method is to use the deleettings. json configuration file, which may be unacceptable for some existing projects with heavy web. cofig configuration.
But the good news is that we can use the existing web. config directly in the. NET Core 2.0 project. This article will introduce in detail the web. config configuration file for. NET Core 2.0 migration. I will not talk about it much below. Let's take a look at the detailed introduction.
Migration Method
1. First introduceSystem.Configuration.ConfigurationManager
Only by introducing it can we make the existing web. config Code read take effect.
2. import web. config file to the project root directory, and change the name to app. config. because. NET Core is essentially a console application, so the ConfigurationManager API reads the app by default. config configuration file instead of web. config configuration file.
3. Remove the content irrelevant to the required configuration in config, mainly<system.web>
,<system.webServer>
And<system.codedom>
And other typical asp.net labels.
Before removal:
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication24-20170824065102.mdf;Initial Catalog=aspnet-WebApplication24-20170824065102;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="MyKey" value="true"/> </appSettings> <system.web> <compilation debug="true" targetFramework="4.7" />
After modification:
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication24-20170824065102.mdf;Initial Catalog=aspnet-WebApplication24-20170824065102;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="MyKey" value="true"/> </appSettings></configuration>
4. Test the original ASP. NET code and view the read configuration value.
using System.Configuration;namespace WebConfigTest.Configuration{ public class ConfigurationService { public static bool GetConfigValue(string key) { var result = false; var val= ConfigurationManager.AppSettings[key]; if (val != null) { result = bool.Parse(val); } return result; } }}
Open a breakpoint to check whether the read configuration value is correct:
As a result, the read configuration value is completely correct.
You can use this method to quickly migrate existing configuration files and code.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.