查看: 1761|回复: 0

[.NET开发] .NET Core 2.0迁移小技巧之web.config 配置文件示例详解

发表于 2018-1-2 08:00:00

前言

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

但是好消息是,我们是可以直接在.NET Core 2.0项目种利用上现有的web.config的。本文将详细介绍.NET Core 2.0迁移之web.config 配置文件的相关内容,下面话不多说了,来一起看看详细的介绍吧。

迁移方法

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

  

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

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

移除前:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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"
  3. providerName="System.Data.SqlClient" /> </connectionStrings>
  4. <appSettings>
  5. <add key="webpages:Version" value="3.0.0.0" />
  6. <add key="webpages:Enabled" value="false" />
  7. <add key="ClientValidationEnabled" value="true" />
  8. <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  9. <add key="MyKey" value="true"/>
  10. </appSettings>
  11. <system.web>
  12. <compilation debug="true" targetFramework="4.7" />
  13. <httpRuntime targetFramework="4.7" />
  14. <httpModules>
  15. <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
  16. </httpModules>
  17. </system.web>
  18. <runtime>
  19. <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  20. <dependentAssembly>
  21. <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
  22. <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
  23. </dependentAssembly>
  24. <dependentAssembly>
  25. <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
  26. <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
  27. </dependentAssembly>
  28. <dependentAssembly>
  29. <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
  30. <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
  31. </dependentAssembly>
  32. <dependentAssembly>
  33. <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
  34. <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  35. </dependentAssembly>
  36. <dependentAssembly>
  37. <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
  38. <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  39. </dependentAssembly>
  40. <dependentAssembly>
  41. <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
  42. <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  43. </dependentAssembly>
  44. </assemblyBinding>
  45. </runtime>
  46. <system.webServer>
  47. <validation validateIntegratedModeConfiguration="false" />
  48. <modules>
  49. <remove name="ApplicationInsightsWebTracking" />
  50. <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
  51. </modules>
  52. </system.webServer>
  53. <system.codedom>
  54. <compilers>
  55. <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" />
  56. <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+" />
  57. </compilers>
  58. </system.codedom>
  59. </configuration>
复制代码

修改后:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3. <configSections>
  4. <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  5. <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  6. </configSections>
  7. <connectionStrings>
  8. <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication24-20170824065102.mdf;Initial Catalog=aspnet-WebApplication24-20170824065102;Integrated Security=True"
  9. providerName="System.Data.SqlClient" />
  10. </connectionStrings>
  11. <appSettings>
  12. <add key="webpages:Version" value="3.0.0.0" />
  13. <add key="webpages:Enabled" value="false" />
  14. <add key="ClientValidationEnabled" value="true" />
  15. <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  16. <add key="MyKey" value="true"/>
  17. </appSettings>
  18. </configuration>
复制代码

4.测试原ASP.NET代码,查看读取配置值

  1. using System.Configuration;
  2. namespace WebConfigTest.Configuration
  3. {
  4. public class ConfigurationService
  5. {
  6. public static bool GetConfigValue(string key)
  7. {
  8. var result = false;
  9. var val= ConfigurationManager.AppSettings[key];
  10. if (val != null)
  11. {
  12. result = bool.Parse(val);
  13. }
  14. return result;
  15. }
  16. }
  17. }
复制代码

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

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

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对程序员之家的支持。



回复

使用道具 举报