全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

前言

相信大家应该都知道.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中和需要的配置无关的内容,主要是<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 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=\&quot;Web\&quot; /optionInfer+" />
 </compilers>
 </system.codedom>
</configuration>

修改后:

<?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.测试原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;
 }
 }
}

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

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

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

总结

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


# .net  # core  # webconfig  # 配置文件  # config  # ASP.NET core Web中使用appsettings.json配置文件的方法  # .NET Core简单读取json配置文件  # .NET Core读取配置文件方式详细总结  # 实现core文件自动生成配置文件的方法  # .NET Core2.1如何获取自定义配置文件信息详解  # 如何在ASP.NET Core类库项目中读取配置文件详解  # Asp.net Core与类库读取配置文件信息的方法  # .Net Core读取Json配置文件的实现示例  # asp.net core配置文件加载过程的深入了解  # ASP.NET Core中修改配置文件后自动加载新配置的方法详解  # 的是  # 相关内容  # 让我们  # 说了  # 不多  # 并将  # 才可以  # 这对  # 大功告成  # 可以直接  # 详细介绍  # 打个  # 这篇文章  # 谢谢大家  # 会去  # 取而代之  # 使用这个  # 移除  # 是否正确 


相关文章: 建站主机如何安装配置?新手必看操作指南  弹幕视频网站制作教程下载,弹幕视频网站是什么意思?  建站主机选择指南:服务器配置与SEO优化实战技巧  ,石家庄四十八中学官网?  制作农业网站的软件,比较好的农业网站推荐一下?  建站之星如何取消后台验证码生成?  如何在IIS服务器上快速部署高效网站?  电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?  官网自助建站平台指南:在线制作、快速建站与模板选择全解析  如何快速完成中国万网建站详细流程?  安云自助建站系统如何快速提升SEO排名?  如何通过FTP空间快速搭建安全高效网站?  建站之星在线客服如何快速接入解答?  如何制作网站标识牌,动态网站如何制作(教程)?  一键制作网站软件下载安装,一键自动采集网页文档制作步骤?  如何选择适合PHP云建站的开源框架?  零服务器AI建站解决方案:快速部署与云端平台低成本实践  ,制作一个手机app网站要多少钱?  如何用搬瓦工VPS快速搭建个人网站?  开封网站制作公司,网络用语开封是什么意思?  建站主机解析:虚拟主机配置与服务器选择指南  深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?  如何选择服务器才能高效搭建专属网站?  如何在Golang中引入测试模块_Golang测试包导入与使用实践  linux top下的 minerd 木马清除方法  如何选择可靠的免备案建站服务器?  上海网站制作网站建设公司,建筑电工证网上查询系统入口?  宁波免费建站如何选择可靠模板与平台?  高防服务器租用指南:配置选择与快速部署攻略  实例解析Array和String方法  韩国服务器如何优化跨境访问实现高效连接?  简易网站制作视频教程,使用记事本编写一个简单的网页html文件?  个人网站制作流程图片大全,个人网站如何注销?  整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?  建站之星安装步骤有哪些常见问题?  专业网站制作企业网站,如何制作一个企业网站,建设网站的基本步骤有哪些?  建站VPS推荐:2025年高性能服务器配置指南  孙琪峥织梦建站教程如何优化数据库安全?  如何快速上传自定义模板至建站之星?  如何选择高效可靠的多用户建站源码资源?  如何通过多用户协作模板快速搭建高效企业网站?  网站制作需要会哪些技术,建立一个网站要花费多少?  网站app免费制作软件,能免费看各大网站视频的手机app?  建站主机选哪家性价比最高?  如何快速搭建自助建站会员专属系统?  如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?  香港服务器网站卡顿?如何解决网络延迟与负载问题?  定制建站如何定义?其核心优势是什么?  移民网站制作流程,怎么看加拿大移民官网?  如何在新浪SAE免费搭建个人博客? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。