前言

相信大家应该都知道.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=\"Web\" /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中修改配置文件后自动加载新配置的方法详解
# 的是
# 相关内容
# 让我们
# 说了
# 不多
# 并将
# 才可以
# 这对
# 大功告成
# 可以直接
# 详细介绍
# 打个
# 这篇文章
# 谢谢大家
# 会去
# 取而代之
# 使用这个
# 移除
# 是否正确
相关文章:
制作表格网站有哪些,线上表格怎么弄?
如何在Golang中使用replace替换模块_指定本地或远程路径
手机网站制作与建设方案,手机网站如何建设?
Python lxml的etree和ElementTree有什么区别
如何登录建站主机?访问步骤全解析
中山网站推广排名,中山信息港登录入口?
如何快速重置建站主机并恢复默认配置?
为什么Go需要go mod文件_Go go mod文件作用说明
山东网站制作公司有哪些,山东大源集团官网?
建站之星后台密码遗忘如何找回?
建站主机SSH密钥生成步骤及常见问题解答?
建站之星24小时客服电话如何获取?
宝塔面板如何快速创建新站点?
设计网站制作公司有哪些,制作网页教程?
详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
seo网站制作优化,网站SEO优化步骤有哪些?
网站制作大概要多少钱一个,做一个平台网站大概多少钱?
详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)
如何快速搭建FTP站点实现文件共享?
c++怎么编写动态链接库dll_c++ __declspec(dllexport)导出与调用【方法】
如何在IIS服务器上快速部署高效网站?
临沂网站制作企业,临沂第三中学官方网站?
如何配置IIS站点权限与局域网访问?
太原网站制作公司有哪些,网约车营运证查询官网?
小建面朝正北,A点实际方位是否存在偏差?
如何通过.red域名打造高辨识度品牌网站?
广州网站制作的公司,现在专门做网站的公司有没有哪几家是比较好的,性价比高,模板也多的?
网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?
建站主机与服务器功能差异如何区分?
建站之星代理平台如何选择最佳方案?
小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建
如何用IIS7快速搭建并优化网站站点?
建站主机默认首页配置指南:核心功能与访问路径优化
如何在阿里云高效完成企业建站全流程?
高性能网站服务器部署指南:稳定运行与安全配置优化方案
如何在万网开始建站?分步指南解析
Android使用GridView实现日历的简单功能
视频网站制作教程,怎么样制作优酷网的小视频?
山东云建站价格为何差异显著?
建站主机服务器选购指南:轻量应用与VPS配置解析
如何在景安云服务器上绑定域名并配置虚拟主机?
如何选择高性价比服务器搭建个人网站?
宝塔面板创建网站无法访问?如何快速排查修复?
建站10G流量真的够用吗?如何应对访问高峰?
IOS倒计时设置UIButton标题title的抖动问题
宝华建站服务条款解析:五站合一功能与SEO优化设置指南
php条件判断怎么写_ifelse和switchcase的使用区别【对比】
网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?
如何在云主机上快速搭建网站?
建站之星云端配置指南:模板选择与SEO优化一键生成
*请认真填写需求信息,我们会在24小时内与您取得联系。