小编典典

Web.Config调试/发布

c#

我知道Visual Studio 2010中的web.config提供了从数据库从调试模式切换到发布模式的功能。

这是我的Web.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>

这是我的Web.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>

这是我的Web.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>

发布项目时,Web.config文件中没有任何显示。它没有显示实时数据库连接字符串吗?


阅读 277

收藏
2020-05-19

共1个答案

小编典典

作为Visual Studio
2010一部分的web.config转换使用XSLT来将当前的web.config文件“转换”为其.Debug或.Release版本。

在.Debug / .Release文件中,您需要在连接字符串字段中添加以下参数:

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

这将导致每个连接字符串行找到匹配的名称并相应地更新属性。

注意:您不必担心更新转换文件中的providerName参数,因为它们不会更改。

这是我的一个应用程序中的一个示例。这是web.config文件部分:

<connectionStrings>
      <add name="EAF" connectionString="Data Source=NTSQLT\S2K5TST;Initial Catalog=HR;User ID=EAFApp;Password=XXXX" providerName="System.Data.SqlClient" />
</connectionString>

这是执行适当转换的web.config.release部分:

<connectionStrings>
      <add name="EAF" connectionString="Data Source=NTSQLP\S2K5TST;Initial Catalog=HR;User ID=EAFApp;Password=YYYY" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>

补充说明:转换仅在发布站点时发生,而不是仅使用F5或CTRL + F5运行时发生。
如果需要在本地对给定的配置运行更新,则必须为此手动更改Web.config文件。

有关更多详细信息,请参见MSDN文档。

https://msdn.microsoft.com/zh-
CN/library/dd465326(VS.100).aspx

2020-05-19