JSON

.NET Core 2.0迁移技巧之web.config配置文件

字号+ 作者:H5之家 来源:H5之家 2017-08-26 09:01 我要评论( )

.NET Core 2.0迁移技巧之web.config配置文件

大家都知道.NET Core现在不再支持原来的web.config配置文件了,取而代之的是json或xml配置文件。官方推荐的项目配置方式是使用appsettings.json配置文件,这对现有一些重度使用web.cofig配置的项目迁移可能是不可接受的。

但是好消息是,我们是可以直接在.NET Core 2.0项目种利用上现有的web.config的。 迁移方法

1.首先在解决方案中引入System.Configuration.ConfigurationManager,只有引入它才可以让我们已有的读取web.config代码起作用.

itdadao

2. 导入web.config文件到项目根目录, 并将名称修改为app.config .  因为.NET Core的项目本质是 控制台应用 ,所以 ConfigurationManager 的API会去默认读取app.config配置文件,而不是web.config配置文件。

itdadao

3.去除config中和需要的配置无关的内容,主要是 <system.web>,<system.webServer>和<system.codedom>等典型asp.net标签 。

移除前:

<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections>     <!-- For more information on Entity Framework configuration, visit ?LinkID=237468 -->     <section type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />   </configSections>   <connectionStrings>     <add 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="http://www.tuicool.com/articles/3.0.0.0" /> <add key="webpages:Enabled" value="http://www.tuicool.com/articles/false" /> <add key="ClientValidationEnabled" value="http://www.tuicool.com/articles/true" /> <add key="UnobtrusiveJavaScriptEnabled" value="http://www.tuicool.com/articles/true" /> <add key="MyKey" value="http://www.tuicool.com/articles/true"/> </appSettings> <system.web> <compilation debug="true" targetFramework="4.7" /> <httpRuntime targetFramework="4.7" /> <httpModules> <add type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> </httpModules> </system.web> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity 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 /> <add 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>

修改后:

<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit ?LinkID=237468 --> <section type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add 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="http://www.tuicool.com/articles/3.0.0.0" /> <add key="webpages:Enabled" value="http://www.tuicool.com/articles/false" /> <add key="ClientValidationEnabled" value="http://www.tuicool.com/articles/true" /> <add key="UnobtrusiveJavaScriptEnabled" value="http://www.tuicool.com/articles/true" /> <add key="MyKey" value="http://www.tuicool.com/articles/true"/> </appSettings> </configuration> 4.测试原ASP.NET代码,查看读取配置值 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; } } }

打个断点,看下读取配置值是否正确:

itdadao

大功告成,读取的配置值完全正确。

大家可以使用这个方法快速迁移现有配置文件和代码过去啦。

Tags:.NET Core

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • jsonasp.net asp.net提取多层嵌套json数据的方法

    jsonasp.net asp.net提取多层嵌套json数据的方法

    2017-08-19 17:04

  • 怎么将JSON字符串转换为.net对象?来

    怎么将JSON字符串转换为.net对象?来

    2017-08-16 12:00

  • Asp.net MVC Razor模板引擎技巧分享

    Asp.net MVC Razor模板引擎技巧分享

    2017-08-09 10:00

  • java接口接受json

    java接口接受json

    2017-07-31 11:06

网友点评