The Web. config configuration file example is detailed

Source: Internet
Author: User
Tags connectionstrings
This article mainly introduces you to the. NET Core 2.0 Migration Tips of the Web. config configuration file, the article through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends to follow the small series to learn together.

Objective

I believe you should know that. NET core now no longer supports the original Web. config configuration file, instead of a JSON or XML configuration file. The official recommended project configuration is to use the Appsettings.json configuration file, which may be unacceptable for some existing project migrations that use Web.cofig configuration heavily.

But the good news is that we can use the existing Web. config directly in the. NET Core 2.0 project. This article will detail the contents of the. NET Core 2.0 Migration's Web. config configuration file, and let's talk a little more about it.

Migration methods

1. First introduced in the solution System.Configuration.ConfigurationManager , only the introduction of it will allow us to read the Web. config code to work.

  

2. Import the Web. config file to the project root directory and modify the name to App. Config. Because the project nature of. NET Core is a console application, the ConfigurationManager API goes to the default to read the app. config configuration file instead of the Web. config profile.

3. Remove config and required configuration-independent content, mainly <system.web> , <system.webServer> and <system.codedom> so on typical ASP.

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. To view the read configuration values


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) {R Esult = bool. Parse (Val); } return result; } }}

Make a breakpoint to see if the Read configuration value is correct:

You are done, read the configuration values exactly right.

You can use this method to quickly migrate existing configuration files and code past.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.