.NET Core 2.0遷移小技巧之web.config 配置文件示例詳解
前言
相信大家應(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代碼起作用.
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配置文件。
3.去除config中和需要的配置無關(guān)的內(nèi)容,主要是<system.web>
, <system.webServer>
和<system.codedom>
等典型asp.net標(biāo)簽。
移除前:
<?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.測(cè)試原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; } } }
打個(gè)斷點(diǎn),看下讀取配置值是否正確:
大功告成,讀取的配置值完全正確。
大家可以使用這個(gè)方法快速遷移現(xiàn)有配置文件和代碼過去啦。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- ASP.NET core Web中使用appsettings.json配置文件的方法
- .NET Core簡(jiǎn)單讀取json配置文件
- .NET Core讀取配置文件方式詳細(xì)總結(jié)
- 實(shí)現(xiàn)core文件自動(dòng)生成配置文件的方法
- .NET Core2.1如何獲取自定義配置文件信息詳解
- 如何在ASP.NET Core類庫項(xiàng)目中讀取配置文件詳解
- Asp.net Core與類庫讀取配置文件信息的方法
- .Net Core讀取Json配置文件的實(shí)現(xiàn)示例
- asp.net core配置文件加載過程的深入了解
- ASP.NET Core中修改配置文件后自動(dòng)加載新配置的方法詳解
相關(guān)文章
Asp.Net實(shí)現(xiàn)FORM認(rèn)證的一些使用技巧(必看篇)
下面小編就為大家?guī)硪黄狝sp.Net實(shí)現(xiàn)FORM認(rèn)證的一些使用技巧(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08asp.net web頁面元素的多語言化(多國(guó)語化)實(shí)現(xiàn)分享
開發(fā)的一些系統(tǒng),經(jīng)常要求支持多語言(例如日文,英文等),接下來介紹如何實(shí)現(xiàn)asp.net開發(fā)中web頁面實(shí)現(xiàn)頁面元素的多語言化(多國(guó)語化)感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)有所幫助2013-02-02ASP.NET?Core中創(chuàng)建中間件的方式匯總
ASP.NET?Core中間件(Middleware)是用于處理HTTP請(qǐng)求和響應(yīng)的組件,它們被安排在請(qǐng)求處理管道中,并按順序執(zhí)行,這篇文章主要介紹了ASP.NET?Core中創(chuàng)建中間件的幾種方式,需要的朋友可以參考下2024-07-07關(guān)于Metalama使用Fabric操作項(xiàng)目或命名空間的問題
Metalama是一個(gè)基于微軟編譯器Roslyn的元編程的庫,可以解決我在開發(fā)中遇到的重復(fù)代碼的問題,這篇文章主要介紹了Metalama使用Fabric操作項(xiàng)目或命名空間,需要的朋友可以參考下2022-04-04asp.net實(shí)現(xiàn)文件無刷新上傳方法匯總
本文給大家介紹的是asp.net實(shí)現(xiàn)文件無刷新上傳的2種方法,分別是使用swfupload插件和uploadify插件,講述的十分細(xì)致全面,附上示例,有需要的小伙伴可以參考下。2015-06-06Asp.net mvc實(shí)時(shí)生成縮率圖到硬盤
這篇文章主要介紹了Asp.net mvc實(shí)時(shí)生成縮率圖到硬盤的相關(guān)資料,需要的朋友可以參考下2016-05-05在GridView中LinkButton的屬性的應(yīng)用(如何不用選中就刪除這一行)
GridView中LinkButton的屬性的應(yīng)用,實(shí)現(xiàn)不用選中就刪除這一行2009-04-04合并網(wǎng)頁中的多個(gè)script引用實(shí)現(xiàn)思路及代碼
為了更好的進(jìn)行封裝,每個(gè)實(shí)現(xiàn)不同功能的js代碼應(yīng)該有自己的js文件,這樣如果一個(gè)網(wǎng)頁中引用了多個(gè)js文件會(huì)很難管理,所以就出現(xiàn)了合并js這以說,感興趣的朋友不妨參考下本文希望對(duì)你有所幫助2013-02-02