>  기사  >  백엔드 개발  >  web.config 구성 파일 예제에 대한 자세한 설명

web.config 구성 파일 예제에 대한 자세한 설명

巴扎黑
巴扎黑원래의
2017-09-01 14:33:593731검색

이 글은 주로 .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를 읽기 위한 기존 코드를 사용할 수 있습니다.System.Configuration.ConfigurationManager,只有引入它才可以让我们已有的读取web.config代码起作用.

  

2. 导入web.config文件到项目根目录,并将名称修改为app.config. 因为.NET Core的项目本质是控制台应用,所以ConfigurationManager的API会去默认读取app.config配置文件,而不是web.config配置文件。

3.去除config中和需要的配置无关的内容,主要是2dc15ec6bc814c3aa45b55d017848bed , 1dcc4c920d99d48189de261dfba48f61455f4645fc0f45153cc77129e16a328a

 

2 web.config 파일을 프로젝트 루트 디렉터리로 가져옵니다. 을 클릭하고 이름을 app.config로 변경합니다. .NET Core 프로젝트는 기본적으로 콘솔 애플리케이션이므로 ConfigurationManager의 API는 기본적으로 web.config 구성 파일 대신 app.config 구성 파일을 읽습니다.


3 관련 없는 구성에서 필수 구성을 제거합니다. <system.web></system.web>, <system.webserver></system.webserver><system.codedom></system.codedom> .net과 같은 주로 일반적인 ASP 콘텐츠 꼬리표.

제거 전:

<?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 구성 파일 예제에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.