RG-*_*G-3 76 c# asp.net web-config
我知道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>
Run Code Online (Sandbox Code Playgroud)
这是我的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>
Run Code Online (Sandbox Code Playgroud)
这是我的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>
Run Code Online (Sandbox Code Playgroud)
当我发布我的项目时,我的Web.config文件中没有显示任何内容.是不是显示我的Live Database连接字符串?
Dil*_*e-O 126
作为Visual Studio 2010使用XSLT的一部分的web.config转换,以便将当前web.config文件"转换"为其.Debug或.Release版本.
在.Debug/.Release文件中,您需要在连接字符串字段中添加以下参数:
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"
Run Code Online (Sandbox Code Playgroud)
这将导致每个连接字符串行找到匹配的名称并相应地更新属性.
注意:您不必担心在转换文件中更新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>
Run Code Online (Sandbox Code Playgroud)
这是执行正确转换的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>
Run Code Online (Sandbox Code Playgroud)
一个补充说明:转换仅在您发布网站时发生,而不是在您使用F5或CTRL + F5运行时发生.如果需要在本地对给定配置运行更新,则必须手动更改Web.config文件.
有关更多详细信息,请参阅MSDN文档
https://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx
可以使用ConfigTransform
构建目标作为Nuget包 - https://www.nuget.org/packages/CodeAssassin.ConfigTransform/
无论选择的构建配置如何,所有"web.* .config"转换文件都将转换并输出为构建输出目录中的一系列"web.*.config.transformed"文件.
这同样适用于非Web项目中的"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>
Run Code Online (Sandbox Code Playgroud)
发布答案,因为这是Google中关于该主题的第一篇Stackoverflow帖子.
要使转换在开发中工作(使用F5或CTRL + F5),我将ctt.exe(https://ctt.codeplex.com/)放在packages文件夹(packages\ConfigTransform\ctt.exe)中.
然后我在Visual Studio中注册一个pre-or build-build事件......
$(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"
Run Code Online (Sandbox Code Playgroud)
对于转换,我使用SlowCheeta VS扩展(https://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5).