可以使用 IIS 7 配置编辑器配置自定义 applicationSettings 部分吗?
我想使用 IIS 7 配置编辑器编辑特定于应用程序的设置。这些设置存储在 applicationSettings
部分中,这是使用 Visual Studio 向项目添加设置时自动添加的类型。我的部分如下所示:
<applicationSettings>
<My.Custom.Properties.Settings>
<setting name="SomePath" serializeAs="String">
<value>D:\Folder\SubFolder</value>
</setting>
</My.Custom.Properties.Settings>
</applicationSettings>
因此,按照此处的指示以及之后经过大量的试验和错误,我能够将该部分加载到 IIS 编辑器中。我可以在 setting
元素上查看(并编辑)属性。我还可以看到 value
元素,但其中的文件夹路径未加载且无法编辑,这正是我需要的!我添加到 IIS 的架构如下所示:
<configSchema>
<sectionSchema name="applicationSettings/My.Custom.Properties.Settings">
<collection addElement="setting">
<attribute name="name" type="string"></attribute>
<attribute name="serializeAs" type="string"></attribute>
<element name="value"></element>
</collection>
</sectionSchema>
</configSchema>
有没有人成功完成了我想做的事情,或者有没有一种方法可以解决这个问题,而无需返回到旧的 appSettings
部分?
I'd like to edit application-specific settings by using the IIS 7 Configuration Editor. The settings are stored in an applicationSettings
section, the kind automatically added for you when using Visual Studio to add settings to a project. My section looks like this:
<applicationSettings>
<My.Custom.Properties.Settings>
<setting name="SomePath" serializeAs="String">
<value>D:\Folder\SubFolder</value>
</setting>
</My.Custom.Properties.Settings>
</applicationSettings>
So, by following directions from here, and after a lot of trial and error, I am able to load the section into the IIS editor. I can see (and edit) the attributes on the setting
element. I can also see the value
element, but the folder path inside it is not loaded and it can't be edited, which is what I need! The schema I have added to IIS looks like this:
<configSchema>
<sectionSchema name="applicationSettings/My.Custom.Properties.Settings">
<collection addElement="setting">
<attribute name="name" type="string"></attribute>
<attribute name="serializeAs" type="string"></attribute>
<element name="value"></element>
</collection>
</sectionSchema>
</configSchema>
Has anybody succeeded in doing what I'm trying to do, or, is there a way around this that doesn't involve going back to the old appSettings
section?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于找不到其他替代方案,我选择更改
applicationSettings
部分的架构,以便将setting
元素的值存储为该元素的属性,而不是另一个元素在其中,这正是 IIS 编辑器所喜欢的。所以,我的设置现在看起来像这样:当然,这意味着实现我自己的设置提供程序(请参阅“自定义设置提供程序”此处),以便我可以读取新属性。
这并不太难,如果您继承 .NET 附带的
LocalFileSettingsProvider
,那就更简单了。此外,如果您不打算在运行时更改设置,则只需实现GetPropertyValues
方法。这种方法的缺点是它使 Visual Studio 的设计时支持变得复杂,因为设置设计器无法识别新架构,而且我找不到告诉它使用自定义提供程序的方法。好消息是,设计器将在向您提供有关此错误的错误后加载,因此您仍然可以使用它来定义设置。但是,当您保存时,它会使用默认架构覆盖您的配置文件。我的解决方案是创建一段在 Visual Studio 的预构建事件上运行的 JScript,它将修改 Visual Studio 更改的设置以符合我的架构。 此页面在编码时非常有用脚本。
实现自定义提供程序的提示:
SettingsPropertyValue.SerializedValue
属性,框架就会为您反序列化实际值。SettingsProviderAttribute
添加到类中。Having found no other alternative, I opted for changing the schema of the
applicationSettings
section so that the value of asetting
element is stored as an attribute of the element, not another element within it, which is what the IIS Editor likes. So, my settings now look like this:This, of course, meant implementing my own settings provider (see "Custom Settings Providers" here) so that I could read from the new attribute.
This is not too hard, and it's even less so if you inherit from the
LocalFileSettingsProvider
that comes with .NET. Moreover, if you don't plan on changing settings at runtime, then you only need to implement theGetPropertyValues
method.The downside with this approach is that it complicates design-time support from Visual Studio, since the Settings designer won't recognize the new schema, and I found no way of telling it to use the custom provider. The good news is that the designer will load after giving you an error about this, so you can still use it to define settings. However, it will overwrite your config file with the default schema when you save. My solution to this was to create a piece of JScript that runs on Visual Studio's prebuild event, and that will modify the settings changed by Visual Studio to conform to my schema. This page was very useful when coding the script.
Tips for implementing the custom provider:
AppDomainSetup.ConfigurationFile
property to get to my config file.SettingsPropertyValue.SerializedValue
property and the framework will deserialize the actual value for you.SettingsProviderAttribute
to the class.