Home  >  Article  >  Backend Development  >  Detailed explanation of web.config configuration file example

Detailed explanation of web.config configuration file example

巴扎黑
巴扎黑Original
2017-09-01 14:33:593731browse

This article mainly introduces you to the relevant information about the web.config configuration file of .NET Core 2.0 migration skills. The article introduces it in detail through sample code, which has certain reference learning value for everyone's study or work. Friends who need it, please follow the editor to learn together.

Preface

I believe everyone should know that .NET Core no longer supports the original web.config configuration file, replaced by json or xml configuration file. The officially recommended project configuration method is to use the appsettings.json configuration file, which may be unacceptable for the migration of some existing projects that heavily use web.cofig configuration.

But the good news is that we can directly use the existing web.config in the .NET Core 2.0 project. This article will introduce in detail the relevant content of the web.config configuration file for .NET Core 2.0 migration. I won’t go into details below, let’s take a look at the detailed introduction.

Migration method

#1. First introduce System.Configuration.ConfigurationManager in the solution, only Introducing it can make our existing web.config reading code work.

 

2. Import the web.config file to the project root directory and change the name to app.config. Because the nature of the .NET Core project is a console application, the API of ConfigurationManager will read the app.config configuration file by default instead of the web.config configuration file.

3. Remove the content in the config that has nothing to do with the required configuration, mainly 2dc15ec6bc814c3aa45b55d017848bed, d9e1bcb4cc01f3462a0bc035148668cf and 455f4645fc0f45153cc77129e16a328a.

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" />
 <httpRuntime targetFramework="4.7" />
 <httpModules>
 <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
 </httpModules>
 </system.web>
 <runtime>
 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
 <dependentAssembly>
 <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
 <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
 </dependentAssembly>
 <dependentAssembly>
 <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
 <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
 </dependentAssembly>
 <dependentAssembly>
 <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
 <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
 </dependentAssembly>
 <dependentAssembly>
 <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
 <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
 </dependentAssembly>
 <dependentAssembly>
 <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
 <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
 </dependentAssembly>
 <dependentAssembly>
 <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
 <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
 </dependentAssembly>
 </assemblyBinding>
 </runtime>
 <system.webServer>
 <validation validateIntegratedModeConfiguration="false" />
 <modules>
 <remove name="ApplicationInsightsWebTracking" />
 <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
 </modules>
 </system.webServer>
 <system.codedom>
 <compilers>
 <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
 <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
 </compilers>
 </system.codedom>
</configuration>

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, check 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;
 }
 }
}

Set a breakpoint to see if the read configuration value is correct:

You're done, the configuration values ​​read are completely correct.

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

The above is the detailed content of Detailed explanation of web.config configuration file example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn