如果我使用 Visual Studio 的设置编辑器,是否有办法防止 web.config 中的注释丢失?
我正在使用 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 设置编辑器,则每当我添加/编辑/删除设置时,注释都会丢失。
到目前为止,我已经想出了以下解决方法:
我可以选择从不使用 VS 设置编辑器,但我必须告诉我的团队也避免使用它,总有那么一次,有人忘记了,我们就丢失了所有评论...
我可以保留 web.config 的单独副本,其中包含一堆评论。然后,我们将应用程序与配置文件的副本一起发送...(我不喜欢这个想法,因为这意味着我必须记住使第二个副本保持最新和 我必须记住在发布时切换配置文件...太多需要记住;太多可能会出错)。
在开始标签上方添加评论似乎没问题,所以我可以将所有评论放在顶部。
我的问题是:解决这个问题的最佳方法是什么?你有什么建议吗?
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:
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...
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).
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方案 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.
我建议您考虑使用 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
我的解决方法 - 这是带有注释的
.config
文件的一部分:此 VB.NET 函数会删除注释并默认删除嵌入的空格
My workaround -- here is part of a
.config
file with comments:This VB.NET function strips the comments and by default removes embedded spaces