简体   繁体   English

在代码中修改Root Web.config

[英]Modifying Root Web.config in code

I would like to store some meta-information about a given site instance that can (a) be managed by that site instance and (b) persist clobbering of Web.config file. 我想存储有关给定站点实例的一些元信息,这些信息可以(a)由该站点实例管理,并且(b)持久破坏Web.config文件。 The site will run in multiple environments (dev,testing,staging and production) and each environment can have different values for this metadata. 该站点将在多个环境(开发,测试,分段和生产)中运行,并且每个环境对此元数据可以具有不同的值。 Note: All environments are running IIS 7.0+ 注意:所有环境都在运行IIS 7.0+

The Root Web.config seems very appealing, as it is certainly outside of the website. Root Web.config似乎非常吸引人,因为它肯定在网站外部。 Therefore, both files and databases can be changed while maintaining the metadata. 因此,可以在维护元数据的同时更改文件和数据库。 I have seen how to modify the appSettings of the Web.config stored in the website, but is it possible to similarly modify the appSettings in the Root Web.config (Specifically within the proper directive)? 我已经看到了如何修改存储在网站中的Web.config的appSettings,但是是否可以类似地修改Root Web.config中的appSettings(特别是在正确的指令内)?

If you have other suggestions of approaching this problem, I would be very happy to hear them. 如果您有解决此问题的其他建议,我将很高兴听到他们的建议。 Thank you! 谢谢!

Yes you can modufy the app settings within your web.config Just use the WebConfigurationManager class in the System.Web.Configuration namespace to create a Configuration object. 是的,您可以在web.config中修改应用程序设置。只需使用System.Web.Configuration命名空间中的WebConfigurationManager类来创建Configuration对象。 This object could then be used to read and write changes to the web.config file. 然后,可以使用该对象读取和写入对web.config文件的更改。

You could then create your own "keys" or attributes that could be read as needed. 然后,您可以创建自己的“键”或可以根据需要读取的属性。

Depending upon what your attributes represent or if they need to be picked up by multiple environmnets from that server I would also look into making the modifications within the machine.config file as then those settings would apply to the enter machine and thereby picked up by multiple environments( if you are hosting multiple webapps from the server). 根据您的属性代表什么,或者是否需要从该服务器的多个环境中获取它们,我还将考虑在machine.config文件中进行修改,因为这些设置将应用于输入计算机,从而被多个用户获取环境(如果您正在从服务器托管多个Web应用程序)。 This could save you time in modifying multiple web.config files and narrorw the storage or the metadata to one location vs. multiple config files in certain situations. 在某些情况下,这可以节省您修改多个web.config文件的时间,并将存储或元数据指向一个位置而不是多个配置文件。

would not use web.config for runtime modifications, that will cause the application to recycle, perhaps some other form of configuration file like app.config 不会使用web.config进行运行时修改,这将导致应用程序回收,也许是其他形式的配置文件,例如app.config

if my assumption is incorrect and the web.config will not be edited after the application is started, then you can use WebConfigurationManager to access the file sections 如果我的假设不正确,并且在应用程序启动后将不会编辑web.config,则可以使用WebConfigurationManager来访问文件部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM