简体   繁体   English

asp.net更改web.config项目的值?

[英]asp.net change value of a web.config item?

im trying to get role provider working in multiple environments and hitting a wall ( link text ) 我试图让角色提供程序在多种环境中工作并碰壁( 链接文本

i should be able to dynamically set the connectionString property of the web.congig item on app_onstart to the correct DB conection String to get it to work... 我应该能够将app_onstart上web.congig项的connectionString属性动态设置为正确的DB连接字符串,以使其正常工作...

can anyone show me how to dynamically alter items in the web.config? 谁能告诉我如何动态更改web.config中的项目? im guessing reflection... 我正在猜测反射...

<roleManager enabled="true" defaultProvider="SqlRoleManager">
    <providers>
        <clear/>
        <add name="SqlRoleManager" type="System.Web.Security.SqlRoleProvider" connectionStringName="ISConnectionString_prod" applicationName="IS"/>
    </providers>
</roleManager>

i want to adjust the connectionStringName value in the above snipet 我想在上面的片段中调整connectionStringName值

thanks 谢谢

If you're using VS2010, you can get it to automatically apply transforms to your config files depending on which environment you're publishing to. 如果您使用的是VS2010,则可以根据发布的环境自动将转换应用于配置文件。

We use this to set connection strings, payment provider config settings (sandbox mode, username, etc.) and a couple of other things like how exceptions are handled. 我们使用它来设置连接字符串,付款提供商的配置设置(沙盒模式,用户名等)以及其他一些诸如异常处理方式。

If you're not publishing, you can actually hook these transforms straight into the build engine by editing the project file. 如果您不发布,则实际上可以通过编辑项目文件将这些转换直接挂钩到构建引擎中。

This makes it incredibly simple to maintain (You have a web.config and a web.Live.config which contains the transforms). 这使维护变得异常简单(您有一个包含转换的web.config和一个web.Live.config )。 It also makes the whole process far less error-prone 这也使整个过程不易出错

eg: 例如:

web.config web.config

  <connectionStrings>
    <clear />
    <add name="OurConnectionString" connectionString="Data Source=DevDbHostname;Initial Catalog=DevDb;user id=Username;password=Password;MultipleActiveResultSets=True" />
  </connectionStrings>

web.Release.config web.Release.config

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="OurConnectionString"
      connectionString="Data Source=LiveDbHostname;Initial Catalog=LiveDb;user id=Username;password=Password;MultipleActiveResultSets=True"
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>
</configuration>

As long as the permissions allow it (you will have to change them), you can treat the web.config just as any other XML file. 只要权限允许(您必须更改它们),就可以将web.config视为任何其他XML文件。 Knowing that, you can simply use an XDocument and pop in a new XElement where you want it. 知道这一点,您可以简单地使用XDocument并在所需的新XElement中弹出它。 But be very careful and be sure to save some backups! 但是要非常小心,并确保保存一些备份!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM