如何在源代码控制下轻松对本地 Web.config 进行特定更改

发布于 2024-10-16 17:46:42 字数 468 浏览 3 评论 0原文

我是负责 ASP.NET Web 应用程序的 10 人团队的一员,但我是卫星办公室(与主办公室位于不同域)的唯一开发人员。在 Web.config 中,我需要进行一些更改,以使 Web 应用程序在我的特定域上工作(对 元素和数据库连接字符串的更改),这些更改与其他人不同我的团队使用。

我无法更改原始的 Web.config 文件,因为这会扰乱我团队中的其他人。 MsBuild Web.config 转换看起来很有希望,但只有在部署 Web 应用程序时才有效(我只是将 IIS 指向我的应用程序的根目录)。我尝试手动运行 构建步骤,但它不会覆盖我的本地 Web.config 文件(访问被拒绝错误)

我可以做什么来轻松地对我的 Web.config 文件进行一些选择性更改本地 Web.config 文件而不修改源代码管理中的内容?

I am part of a 10 person team working on an ASP.NET web application, but am the only developer in a satellite office (which is on a different domain than the main office). In the Web.config, I need to make alterations to have the web application work on my specific domain (alterations to the <identity> element and to database connection strings) that are different than everyone else on my team uses.

I can't alter the original Web.config file, since that would mess up everyone else on my team. The MsBuild Web.config transformations looked promising, but only work if you deploy the web application (I'm just pointing IIS at my application's root directory). I explored manually running a <TransformXml> build step, but it won't overwrite my local Web.config file (Access is Denied error)

What can I do to easily make a few selective changes to my local Web.config file without modifying what's in source control?

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

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

发布评论

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

评论(2

无言温柔 2024-10-23 17:46:42

需要本地更改的最常见部分是 connectionStringsappSettings。对于这两个部分,.NET 框架允许存储在单独的文件中。

然后,您只需将这些单独的文件置于源代码控制之外(忽略、获取一次然后隐藏、禁用文件版本控制......无论您使用什么风格的源代码控制)。

例如,

<configuration>
    <connectionStrings configSource="connections.config"/>
</configuration>


<connectionStrings>
    <add name="Name"  
     providerName="System.Data.ProviderName" 
     connectionString="My;Personal;Connection;String;" />
</connectionStrings>

The most common sections to require local changes are connectionStrings and appSettings. For both of these sections, the .NET framework allows for storage in a separate file.

You then simply keep these separate file out of source control (ignore, get once then cloak, disable versioning on file... whatever flavour source control you use).

For example,

<configuration>
    <connectionStrings configSource="connections.config"/>
</configuration>


<connectionStrings>
    <add name="Name"  
     providerName="System.Data.ProviderName" 
     connectionString="My;Personal;Connection;String;" />
</connectionStrings>
硬不硬你别怂 2024-10-23 17:46:42

我不知道有什么解决方案不会修改源代码管理中的内容。但您可以考虑以下一些可能适合您情况的选项:

Global.asax 条件设置:

void Session_Start(object sender, EventArgs e)
{
  if (HttpContext.Current.Server.MachineName == "JOE"  ){
    //my development machine
    Application["mysetting"] = "myspecial setting"; 
  }else{
    // ... 
  }
}

多个 web.config

有时,不同的服务器有不同的 web.config。管理每个设置可能会成为一场噩梦,因此有关堆栈溢出的问题(找不到它!抱歉)建议这样做,其中复制粘贴了正确的 web.config.根据需要进入主 web.config 文件。

Application/
  web.config.devserver
  web.config.joebrown
  web.config.production

动态更改 web.config 设置

有关更多详细信息,请参阅 Stack Overflow 上的帖子:asp.net 管理多个 web.config 文件

public static string GetConnString()
{
    string connString = ConfigurationSettings.AppSettings[GetConfigKey("database")];
    return connString;
}

public static string GetConfigKey(string baseKey)
{
    string str = baseKey;
    if (Dns.GetHostName().StartsWith("dinoch"))
    {
        str = str + "-dev";
    }
    else if (Dns.GetHostName().StartsWith("prodsrvr"))
    {
        str = str + "-prod";
    }
    return str;
}

<configuration>
   <appSettings>
     <add key="database-dev" value="server=(local)\vsdotnet;database=ASPXAPPS;Integrated Security=SSPI" />
     <add key="database-prod" value="server=(local)\vsdotnet;database=ASPXAPPS;Integrated Security=SSPI" />
   </appSettings>
</configuration>

I don't know of any solutions that don't modify what's in Source Control. But here are some options you can consider that might work in your situation:

Global.asax conditional settings:

void Session_Start(object sender, EventArgs e)
{
  if (HttpContext.Current.Server.MachineName == "JOE"  ){
    //my development machine
    Application["mysetting"] = "myspecial setting"; 
  }else{
    // ... 
  }
}

Multiple web.configs

Sometimes you have different web.configs for different servers. Managing the settings in each can become a nightmare, so a question on stack overflow (can't find it! sorry) suggested this, where the proper web.config.<setting> is copy-pasted into the main web.config file as appropriate.

Application/
  web.config.devserver
  web.config.joebrown
  web.config.production

Change your web.config settings dynamically

See a post on Stack Overflow for more details: asp.net managing multiple web.config files

public static string GetConnString()
{
    string connString = ConfigurationSettings.AppSettings[GetConfigKey("database")];
    return connString;
}

public static string GetConfigKey(string baseKey)
{
    string str = baseKey;
    if (Dns.GetHostName().StartsWith("dinoch"))
    {
        str = str + "-dev";
    }
    else if (Dns.GetHostName().StartsWith("prodsrvr"))
    {
        str = str + "-prod";
    }
    return str;
}

<configuration>
   <appSettings>
     <add key="database-dev" value="server=(local)\vsdotnet;database=ASPXAPPS;Integrated Security=SSPI" />
     <add key="database-prod" value="server=(local)\vsdotnet;database=ASPXAPPS;Integrated Security=SSPI" />
   </appSettings>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文