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

发布于 2024-10-02 14:21:36 字数 694 浏览 1 评论 0原文

我试图让角色提供者在多种环境中工作并碰壁 (链接文本

我应该能够动态地将app_onstart上的web.congig项目的connectionString属性设置为正确的数据库连接字符串以使其正常工作...

任何人都可以告诉我如何动态更改web.config中的项目吗? 我猜测反射...

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

我想调整上面片段中的connectionStringName值,

谢谢

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...

can anyone show me how to dynamically alter items in the 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

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛徒 2024-10-09 14:21:36

如果您使用的是 VS2010,则可以将其访问 根据您要发布到的环境自动将转换应用于您的配置文件。

我们用它来设置连接字符串、支付提供商配置设置(沙盒模式、用户名等)以及其他一些内容,例如如何处理异常。

如果您不进行发布,实际上可以通过编辑项目文件将这些转换直接挂接到构建引擎中。

这使得维护变得非常简单(您有一个 web.config 和一个包含转换的 web.Live.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

<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>

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.

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). It also makes the whole process far less error-prone

eg:

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

<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>
帅气尐潴 2024-10-09 14:21:36

只要权限允许(您必须更改它们),您就可以像对待任何其他 XML 文件一样对待 web.config。知道了这一点,您可以简单地使用 XDocument 并在您想要的位置弹出一个新的 XElement 。但要非常小心,一定要保存一些备份!

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. Knowing that, you can simply use an XDocument and pop in a new XElement where you want it. But be very careful and be sure to save some backups!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文