web.config 配置文件示例详解【C#.Net教程】,web.config,iis伪静态,配置文件
作者:搜教程发布时间:2019-11-27分类:.Net教程浏览:28评论:0
媒介
置信人人应当都晓得.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; } } }
打个断点,看下读取设置值是不是准确:
功德圆满,读取的设置值完全准确。
人人能够运用这个要领疾速迁徙现有设置文件和代码过去啦。
以上就是web.config 设置文件示例详解的细致内容,更多请关注ki4网别的相干文章!
相关推荐
- .Net 设置文件——继续ConfigurationSection完成自定义处置惩罚类处置惩罚自定义设置节点【C#.Net教程】,.Net ,配置文件,ConfigurationSection
- php的配置文件是哪两个【php问题】,php,配置文件
- ASP.NET中的Web.config配置文件引见【C#.Net教程】,web.config,Web.config,ASP.NET
- C#中INI配置文件的图文代码详解【C#.Net教程】,csharp,配置文件,.net
- 织梦cms数据库配置文件在哪【CMS教程】,织梦,数据库,配置文件
- session的存储体式格局和配置文件【C#.Net教程】,session,存储方式,配置文件
- C# 操纵配置文件 App.config的详解【C#.Net教程】,C# ,配置文件,App.config
- dedecms怎样配置文件【CMS教程】,dedecms,配置文件
- .Net配置文件——反射+配置文件存储范例实例【C#.Net教程】,.Net,配置文件,反射
- .Net配置文件——一致节点配置管理【C#.Net教程】,.Net,配置文件,配置管理
你 发表评论:
欢迎- .Net教程排行
-
- 1案例分享c++ map的运用和 查找机能测试【C#.Net教程】,性能,map,c++
- 2细致引见C# string花样的日期时候字符串转为DateTime范例的要领【C#.Net教程】,C#,string,DateTime
- 3详解ASP.NET中衔接数据库设置要领【C#.Net教程】,ASP.NET,数据库,配置
- 4c#怎样运用?c#的基础语法【C#.Net教程】,c#,关键字
- 5C# DataSet机能最好实践【C#.Net教程】,C#,DataSet
- 6C#_挪用封装的一个类完成导出Excel表格的功用【C#.Net教程】,C# Excel表格
- 7让WebAPI 返回JSON花样的数据实例教程【C#.Net教程】,javascript,WebAPI,JSON,api,web,搭建,返回
- 8在C++中对象怎样作为参数通报和返回?(代码示例)【C#.Net教程】,C++,对象
- 9.net和c#有什么区别【C#.Net教程】,.net,c#
- 最新文章
- 广而告之