source

Web.Config 디버그/릴리스

factcode 2023. 6. 8. 22:37
반응형

Web.Config 디버그/릴리스

Visual Studio 2010의 web.config는 데이터베이스를 디버그 모드에서 릴리스 모드로 전환할 수 있는 기능을 제공하는 것으로 알고 있습니다.

여기 내 웹이 있습니다.Release.config:

<?xml version="1.0"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient" />
    <add name="Testing1" connectionString="Data Source=test;Initial Catalog=TestDatabase;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>

</configuration>

여기 내 웹이 있습니다.debug.config 코드:

<?xml version="1.0"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient" />
    <add name="Live1" connectionString="Data Source=Live;Initial Catalog=LiveDatabase;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>

</configuration>

다음은 제 웹.config 코드입니다.

<?xml version="1.0"?>

<!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 -->
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <authentication mode="Forms">
       <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
       <providers>
          <clear/>
          <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
       </providers>
    </membership>

    <profile>
       <providers>
          <clear/>
          <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
       </providers>
    </profile>

    <roleManager enabled="false">
       <providers>
          <clear/>
          <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
       </providers>
    </roleManager>

  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

프로젝트를 게시할 때 웹.config file.it 에 표시되는 내용이 없습니다. Live Database 연결 문자열이 표시되지 않습니까?

Visual Studio 2010의 일부인 web.config 변환은 XSLT를 사용하여 현재 web.config 파일을 로 "변환"합니다.디버그 또는 .릴리스 버전.

당신 안에.디버그/.파일을 릴리스합니다. 연결 문자열 필드에 다음 매개 변수를 추가해야 합니다.

xdt:Transform="SetAttributes" xdt:Locator="Match(name)"

이렇게 하면 각 연결 문자열 행이 일치하는 이름을 찾고 그에 따라 속성을 업데이트합니다.

참고: 변환 파일의 providerName 매개 변수는 변경되지 않으므로 업데이트할 걱정이 없습니다.

여기 제 앱 중 하나의 예가 있습니다.다음은 web.config 파일 섹션입니다.

<connectionStrings>
      <add name="EAF" connectionString="[Test Connection String]" />
</connectionString>

다음은 web.config.release 섹션에서 적절한 변환을 수행하는 것입니다.

<connectionStrings>
      <add name="EAF" connectionString="[Prod Connection String]"
           xdt:Transform="SetAttributes"
           xdt:Locator="Match(name)" />
</connectionStrings>

추가된 노트 하나:변환은 사이트를 게시할 때만 수행되며 F5 또는 Ctrl+F5로 실행할 때만 수행되지 않습니다.지정된 구성에 대해 로컬로 업데이트를 실행해야 하는 경우 이를 위해 Web.config 파일을 수동으로 변경해야 합니다.

자세한 내용은 MSDN 설명서를 참조하십시오.

https://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

다음을 사용하여 가능합니다.ConfigTransformNuget 패키지로 제공되는 빌드 대상 - https://www.nuget.org/packages/CodeAssassin.ConfigTransform/

모든 "web.*config" 변환 파일은 변환되고 선택한 빌드 구성에 관계없이 빌드 출력 디렉터리에 있는 일련의 "web.*config.transformed" 파일로 출력됩니다.

웹 프로젝트가 아닌 "app.*.config" 변환 파일에도 동일하게 적용됩니다.

그런 다음 다음 대상을 추가합니다.*.csproj.

<Target Name="TransformActiveConfiguration" Condition="Exists('$(ProjectDir)/Web.$(Configuration).config')" BeforeTargets="Compile" >
    <TransformXml Source="$(ProjectDir)/Web.Config" Transform="$(ProjectDir)/Web.$(Configuration).config" Destination="$(TargetDir)/Web.config" />
</Target>

이것으로 답변을 게시하는 것은 해당 주제에 대해 Google에 나타나는 첫 번째 스택 오버플로 게시물입니다.

(F5 또는 Ctrl+F5 사용) 개발 중인 변환을 수행하기 위해 패키지 폴더(packages\ConfigTransform\ctt.exe)에 ctt.exe(https://ctt.codeplex.com/) 를 놓습니다.

그런 다음 빌드 전 또는 빌드 후 이벤트를 Visual Studio에 등록합니다.

$(SolutionDir)packages\ConfigTransform\ctt.exe source:"$(ProjectDir)connectionStrings.config" transform:"$(ProjectDir)connectionStrings.$(ConfigurationName).config" destination:"$(ProjectDir)connectionStrings.config"
$(SolutionDir)packages\ConfigTransform\ctt.exe source:"$(ProjectDir)web.config" transform:"$(ProjectDir)web.$(ConfigurationName).config" destination:"$(ProjectDir)web.config"

변환을 위해 저는 SlowCheeta VS 확장(https://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5) 을 사용합니다.

프로덕션 환경에서 모든 연결 문자열을 새 연결 문자열로 바꾸려면 다음 구문을 사용하여 모든 연결 문자열을 프로덕션 문자열로 바꾸기만 하면 됩니다.

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

<connectionStrings xdt:Transform="Replace">
    <!-- production environment config --->
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient" />
    <add name="Testing1" connectionString="Data Source=test;Initial Catalog=TestDatabase;Integrated Security=True"
      providerName="System.Data.SqlClient" />
</connectionStrings>
....

이 답변에 대한 정보는 이 답변과 이 블로그 게시물에서 가져옵니다.

주의: 다른 사람들이 이미 설명했듯이, 이 설정은 응용 프로그램을 실행/디버깅할 때(F5 키를 눌러)가 아닌 게시할 때만 적용됩니다.

언급URL : https://stackoverflow.com/questions/5811305/web-config-debug-release

반응형