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

但是好消息是,我们是可以直接在.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中和需要的配置无关的内容,主要是<system.web>,<system.webServer>和<system.codedom>等典型asp.net标签。

    移除前:

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

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

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

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

.NET Core 2.0迁移技巧之web.config配置文件的更多相关文章

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

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

  2. .NET Core 2.0迁移技巧之MemoryCache问题修复

    对于传统的.NET Framework项目而言,System.Runtime.Caching命名空间是常用的工具了,其中MemoryCache类则常被用于实现内存缓存. .NET Core 2.0暂时 ...

  3. asp.net 2.0中新增的web.config的默认namespace功能 (转)

    看上去这个题目比较长,但实际上,我在看资料时发现,这就是说,在asp.net 2.0中,只需要在web.config里定义你要用的那些namespace,则在aspx页面中就不需要再象1.1那样,用 ...

  4. NET Core 导入web.config配置文件

    1.首先需要在NET Core项目中引入System.Configuration.ConfigurationManager,它可以读取web.config中的相关配置信息. 2. 将web.confi ...

  5. Web.config配置文件详解

    整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <?xml v ...

  6. [转]Web.config配置文件详解(新手必看)

    本文转自:http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html 花了点时间整理了一下ASP.NET Web.config配 ...

  7. 转:Web.config配置文件详解(新手必看)

    转:http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html 花了点时间整理了一下ASP.NET Web.config配置文件 ...

  8. Web.config配置文件详解(新手必看)

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  9. .net Web.Config配置文件 转

    .net Web.Config配置文件 博客分类: .net   .net Web.Config配置文件 一.配置信息 <?xml version="1.0" encodin ...

随机推荐

  1. [原创]Vivado SDK Initializing s/w repositories不动

    21:56:16 INFO : Launching XSDB server: xsdb.bat C:/Xilinx/SDK/2015.4/scripts/xsdb/xsdb/xsdb-server.t ...

  2. qqluxc

    因为现在noi/noip都是无限栈 noi-linux开栈指令 ulimit -s 102400 这个是100mb 平衡树*2 维护序列 翻转 平衡树+1 维护区间+* t了3个点.. 注意打完标记 ...

  3. 洛谷 p5020 货币系统 题解

    传送门 一个手动枚举能过一半点而且基本靠数学的题目(然而我考试的时候只有25分) 读清题目后发现就是凑数嘛,.... 对啊,就是凑数,怎么凑是重点啊.. 于是就绝望了一小时手动枚举n从1到5的情况 吐 ...

  4. Hadoop的数据压缩

    一.Hadoop的数据压缩 1.概述 在进行MR程序的过程中,在Mapper和Reducer端会发生大量的数据传输和磁盘IO,如果在这个过程中对数据进行压缩处理,可以有效的减少底层存储(HDFS)读写 ...

  5. Spring常用注解总结

    转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...

  6. 个人总结ASP.NET必备面试题

    1.你能解释下MVC的完整流程吗? 所有的终端用户请求被发送到控制器.控制器依赖请求去选择加载哪个模型,并把模型附加到对应的视图.附加了模型数据的最终视图做为响应发送给终端用户. 2. 那你说一下你对 ...

  7. 网络攻击技术:SQL Injection(sql注入)

    网络攻击技术开篇——SQL Injection   1.1.1 摘要 日前,国内最大的程序员社区CSDN网站的用户数据库被黑客公开发布,600万用户的登录名及密码被公开泄露,随后又有多家网站的用户密码 ...

  8. vue插件官方文档,做个记录

    vue的插件,组件都可以按照这种方式添加 官方文档 https://cn.vuejs.org/v2/guide/plugins.html 做个记录用

  9. OI暑假集训游记

    莞中OI集训游记 Written BY Jum Leon. I        又是一载夏,本蒟蒻以特长生考入莞中,怀着忐忑的心情到了8月,是集训之际.怀着对算法学习的向往心情被大佬暴虐的一丝恐惧来到了 ...

  10. C#中IPAddress转换成整型int

    string addr = "11.22.33.44"; System.Net.IPAddress IPAddr=System.Net.IPAddress.Parse(addr); ...