这篇文章主要给大家介绍了关于.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中和需要的配置无关的内容,主要是2dc15ec6bc814c3aa45b55d017848bed , 1dcc4c920d99d48189de261dfba48f61和455f4645fc0f45153cc77129e16a328a等典型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;
}
}
}
打个断点,看下读取配置值是否正确:
大功告成,读取的配置值完全正确。
大家可以使用这个方法快速迁移现有配置文件和代码过去啦。
以上就是web.config 配置文件示例详解的详细内容。