如果我使用 Visual Studio 的设置编辑器,是否有办法防止 web.config 中的注释丢失?

发布于 2024-12-14 19:24:13 字数 956 浏览 1 评论 0原文

我正在使用 Visual Studio 2010 中的设置编辑器在 .NET 4 web.config 文件部分添加/编辑/删除我的设置。

下面是一个(相当模糊的)示例摘录,用于说明我的意思:

  <applicationSettings>
    <Animals.My.MySettings>
      <!-- Specify the type of animal this website is dedicated to... -->
      <setting name="AnimalType" serializeAs="String">
        <value>Monkey</value>
      </setting>
    </Animals.My.MySettings>
  </applicationSettings>

在上面的示例中,注释对于任何在现场手动编辑配置文件的人(例如,一旦 Web 应用程序已部署到客户站点)都是有用的。但是,如果我使用 VS 设置编辑器,则每当我添加/编辑/删除设置时,注释都会丢失。

到目前为止,我已经想出了以下解决方法:

  1. 我可以选择从不使用 VS 设置编辑器,但我必须告诉我的团队也避免使用它,总有那么一次,有人忘记了,我们就丢失了所有评论...

  2. 我可以保留 web.config 的单独副本,其中包含一堆评论。然后,我们将应用程序与配置文件的副本一起发送...(我不喜欢这个想法,因为这意味着我必须记住使第二个副本保持最新 我必须记住在发布时切换配置文件...太多需要记住;太多可能会出错)。

  3. 在开始标签上方添加评论似乎没问题,所以我可以将所有评论放在顶部。

我的问题是:解决这个问题的最佳方法是什么?你有什么建议吗?

I'm using the settings editor in Visual Studio 2010 to add/edit/remove my settings from the section of my .NET 4 web.config file.

Here's a (pretty vague) example extract to illustrate the bit I mean:

  <applicationSettings>
    <Animals.My.MySettings>
      <!-- Specify the type of animal this website is dedicated to... -->
      <setting name="AnimalType" serializeAs="String">
        <value>Monkey</value>
      </setting>
    </Animals.My.MySettings>
  </applicationSettings>

In the example above, the comment is useful for anyone manually editing the config file on-site (e.g. once the web application has been deployed to the customer site). However, if I use the VS settings editor, the comment is lost whenever I add/edit/remove a setting.

So far, I've come up with the following work-arounds:

  1. I could choose to never use the VS settings editor, but I'd have to tell my team to avoid it too, and there's always the one time that someone forgets and we lose all our comments...

  2. I could keep a separate copy of the web.config, with a bunch of comments in it. We then ship the application with the copy of the config file... (I don't like this idea, because it means I've got to remember to keep the second copy up-to-date and I have to remember to switch the config files on release... Too much to remember; too much could go wrong).

  3. Adding comments above the opening tag seems to be OK, so I could just have all my comments at the top.

My question is: What's the best way to work around this problem? What do you recommend?

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

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

发布评论

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

评论(3

与君绝 2024-12-21 19:24:13

方案 1 和方案 2 都面临着同样的问题:团队纪律。 1 意味着他们无法使用工作室编辑器,2 意味着他们必须记住保持“评论”配置同步。

就我个人而言,我认为第一个更容易执行,如果您使用源代码控制,那么违规者是谁就非常明显了。

Option 1 and 2 suffer from the same problem: team discipline. 1 means they can't use the studio editor, and 2 means they have to remember to keep the "comment" config in sync.

Personally, I think the first one is easier to enforce and if you are using source control, then it's pretty obvious who the offenders are.

兔小萌 2024-12-21 19:24:13

我建议您考虑使用 ConfigGen。本页上的示例可能会给您一些好主意。

从本质上讲,它为您提供了一个位置来编辑所有环境的配置,使用标记化占位符和一个设置文件,其中每个部署环境或开发机器都有一行。您可以逐台机器或使用布尔条件包含/排除机器上的注释。

另外,看看我在这个答案中发布的用法示例...

如何为多个构建配置选择不同的app.config

I would recommend that you look at generating all of your environment-specific config, including environment-specific comments, using ConfigGen. The examples on this page might give you some good ideas.

Essentially, it gives you a single place to edit your config for all of your environments, using tokenised placeholders and a settings file with a row for each deployment environment or dev machine. You can include/exclude comments on a machine by machine basis or by using a boolean condition.

Also, take a look at the example of usage I posted in this answer...

How to select different app.config for several build configurations

丢了幸福的猪 2024-12-21 19:24:13

我的解决方法 - 这是带有注释的 .config 文件的一部分:

<setting name="Database" serializeAs="String">
<value>DBNAME</value>
</setting>
<setting name="ProgramPath" serializeAs="String">
<value>C:\Program Files (x86)\Vendor\Program V 11.1\prog.exe:: the P21 client -  KEEP SPACE in a comment will not remove spaces from the value</value>
</setting>
<setting name="Directory" serializeAs="String">
<value>\\Server\Share\Subdir :: where the files will be created</value>
</setting>
<setting name="FileIDs" serializeAs="String">
<value>DoThis, ThenThis :: DoThis or ThenThis or both comma separated - anything after two colons is a comment and is ignored</value>
</setting>

此 VB.NET 函数会删除注释并默认删除嵌入的空格

Function Run(S As String) As String
    Dim L As Integer = InStr(S, "::")
    If L = 0 Then
        Return S.Replace(Space(1), String.Empty)
    Else
        If InStr(S, "KEEP SPACE") = 0 Then
            Return S.Substring(0, L - 1).Replace(Space(1), String.Empty)
        Else
            Return S.Substring(0, L - 1)
        End If
    End If
End Function

My workaround -- here is part of a .config file with comments:

<setting name="Database" serializeAs="String">
<value>DBNAME</value>
</setting>
<setting name="ProgramPath" serializeAs="String">
<value>C:\Program Files (x86)\Vendor\Program V 11.1\prog.exe:: the P21 client -  KEEP SPACE in a comment will not remove spaces from the value</value>
</setting>
<setting name="Directory" serializeAs="String">
<value>\\Server\Share\Subdir :: where the files will be created</value>
</setting>
<setting name="FileIDs" serializeAs="String">
<value>DoThis, ThenThis :: DoThis or ThenThis or both comma separated - anything after two colons is a comment and is ignored</value>
</setting>

This VB.NET function strips the comments and by default removes embedded spaces

Function Run(S As String) As String
    Dim L As Integer = InStr(S, "::")
    If L = 0 Then
        Return S.Replace(Space(1), String.Empty)
    Else
        If InStr(S, "KEEP SPACE") = 0 Then
            Return S.Substring(0, L - 1).Replace(Space(1), String.Empty)
        Else
            Return S.Substring(0, L - 1)
        End If
    End If
End Function

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