一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP.NET教程 - .NET Core 2.0遷移小技巧之web.config 配置文件示例詳解

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

2020-05-14 12:41msp的昌偉哥哥 ASP.NET教程

這篇文章主要給大家介紹了關(guān)于.NET Core 2.0遷移技巧之web.config 配置文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

相信大家應(yīng)該都知道.NET Core現(xiàn)在不再支持原來的web.config配置文件了,取而代之的是json或xml配置文件。官方推薦的項(xiàng)目配置方式是使用appsettings.json配置文件,這對(duì)現(xiàn)有一些重度使用web.cofig配置的項(xiàng)目遷移可能是不可接受的。

但是好消息是,我們是可以直接在.NET Core 2.0項(xiàng)目種利用上現(xiàn)有的web.config的。本文將詳細(xì)介紹.NET Core 2.0遷移之web.config 配置文件的相關(guān)內(nèi)容,下面話不多說了,來一起看看詳細(xì)的介紹吧。

遷移方法

1.首先在解決方案中引入System.Configuration.ConfigurationManager,只有引入它才可以讓我們已有的讀取web.config代碼起作用.

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

2. 導(dǎo)入web.config文件到項(xiàng)目根目錄,并將名稱修改為app.config. 因?yàn)?NET Core的項(xiàng)目本質(zhì)是控制臺(tái)應(yīng)用,所以ConfigurationManager的API會(huì)去默認(rèn)讀取app.config配置文件,而不是web.config配置文件。

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

3.去除config中和需要的配置無關(guān)的內(nèi)容,主要是<system.web> , <system.webServer><system.codedom>等典型asp.net標(biāo)簽。

移除前:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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>

修改后:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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代碼,查看讀取配置值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
 }
 }
}

打個(gè)斷點(diǎn),看下讀取配置值是否正確:

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

大功告成,讀取的配置值完全正確。

大家可以使用這個(gè)方法快速遷移現(xiàn)有配置文件和代碼過去啦。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。

原文鏈接:http://www.cnblogs.com/mantgh/p/7425113.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 武侠古典久久亚洲精品 | 日本不卡在线一区二区三区视频 | 91久久综合 | chinese国产打屁股 | 乌克兰13一14娇小 | 日韩精品一区二区 | 日本制服丝袜 | 四虎tv在线观看884aa | 秋葵丝瓜茄子草莓榴莲樱桃 | 能免费观看的韩剧 | 国产成人无精品久久久久国语 | 精品国产免费久久久久久 | 精品国语对白精品自拍视 | 国产区综合另类亚洲欧美 | 亚洲天堂一区二区在线观看 | 美女做又爽又黄又猛 | 激情三级做爰在线观看激情 | 国内剧情麻豆 | 四虎私人影院 | 免费视频片在线观看大片 | 日本色资源 | 国产一卡二卡3卡4卡四卡在线视频 | 亚洲卡一卡2卡三卡4卡无卡三 | 成年人免费在线看的惊悚动作片 | 久久久久久久尹人综合网亚洲 | 999精品视频在线观看热6 | 欧美日韩国产一区二区三区伦 | 国产一区二区免费在线 | 丝瓜视频黄瓜视频 | 99国产牛牛视频在线网站 | 大陆日韩欧美 | 午夜理论电影在线观看亚洲 | 非洲黑人xxxxxbbbbb | 亚洲精品久久7777777 | 麻豆网站在线免费观看 | 91亚洲一区二区在线观看不卡 | 国产一区二区精品 | hd性欧美俱乐部中文 | 三级黄色片在线观看 | 操妈小说| 精品国产免费第一区二区 |