Today, I was told to create a node to modify web. config (modify the value of a node in system. Web/caching/outputcachesettings)
I wanted to serialize it using an XML file, but I felt that I had to rewrite allCode, Trouble! Google directly finds only one method to modify the appsettings and connectionstrings nodes.) Well, the requirement is similar, but the nodes are different and the depth is different. The change can still be used.
In the original method, the appsonstrings and appsonstrings nodes are at the configuration level of Web. config, which is relatively simple and can be directly used.
Using system. Web. configuration;
Using system. configuration;
Configuration Config = system. web. configuration. webconfigurationmanager. openwebconfiguration (httpcontext. current. request. applicationpath); etettingssection paietting = (appsettingssection) config. getsection ("appsettings"); connectionstringssection connectionsetting = (connectionstringssection) config. getsection ("connectionstrings ");
However, the system. Web/caching/outputcachesettings/outputcacheprofiles cannot find objects such as system. configurationconfigurationconfigurettingssection for you to operate outputcacheprofiles, offering the powerful msdn of F1
Finally, find the corresponding method: you cannot find outputcacheprofiles directly, but you can operate outputcacheprofiles by finding the outputcachesettings object (the object corresponding to the parent node of outputcacheprofiles ).
Core code for operating system. Web/caching/outputcachesettings/outputcacheprofiles:
/// <Summary> /// Add a database connection string node. If this node already exists, the value of the node is modified. // </Summary> /// <Param name = "name"> </param> /// <Param name = "duration"> </param> Public void addoutputcacheprofiles (string name, int duration) {// here the divergence is: modify web. the remaining config nodes are similar to the outputcachesettingssection outputcachesetattributes = (outputcachesettingssection) config. getsection ("System. Web/caching/outputcachesettings");
// The hierarchy of outputcachesettings in Web. config. The root directory is configuration.
If (outputcachesettings. outputcacheprofiles [name] = NULL) // if this node does not exist, add {outputcacheprofile outputcacheprofiles = new outputcacheprofile (name); outputcacheprofiles. duration = duration; outputcacheprofiles. location = system. web. UI. outputcachelocation. any; outputcacheprofiles. varybyparam = "*"; // currently, only one duration parameter is modified. Therefore, the method only has one parameter. To modify all parameters, expand outputcachesettings. outputcacheprofiles. add (outputcacheprofiles);} else // if this node exists, modify {modifyoutputcacheprofiles (name, duration); }/// <summary> // modify the application Program Configuration node. If this node does not exist, the node and corresponding value are added. /// </Summary> /// <Param name = "name"> </param> /// <Param name = "newduration "> </param> Public void modifyoutputcacheprofiles (string name, int newduration) {outputcachesettingssection outputcachesettings = (outputcachesettingssection) config. getsection ("system. web/caching/outputcachesettings "); If (outputcachesettings. outputcacheprofiles [name]! = NULL) // if this node does not exist, add {outputcachesettings. outputcacheprofiles [name]. duration = newduration;} else // if this node does not exist, add {addoutputcacheprofiles (name, newduration );}}
Structure of Web. config
<? XML version = "1.0"?> <! -- Note: In addition to manually editing this file, you can also use web management tools to configure application settings. You can use the "website"> "ASP. NET configuration" option in Visual Studio. The complete list of settings and comments is displayed on the machine. config. in comments, the file is usually located in \ windows \ Microsoft. net \ framework \ v2.x \ config --> <configuration> <etettings/> <connectionstrings/> <system. web> <! -- Set compilation DEBUG = "true" to insert the debugging symbol into the compiled page. However, this affects performance, so this value is set to true only during development. --> <Caching> <outputcachesettings> <outputcacheprofiles> <Add name = "webcache" Duration = "600" location = "any" varybyparam = "*"/> </outputcacheprofiles> </outputcachesettings> </caching> <compilation DEBUG = "true"> <assemblies> <add Assembly = "system. windows. forms, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "/> </assemblies> </compilation> <! -- In the <authentication> section, you can configure the Security Authentication mode used by ASP. NET to identify the passed-in user. --> <Authentication mode = "Windows"/> <! -- If an unprocessed error occurs during request execution, you can configure the corresponding processing steps in the <mermerrors> section. Specifically, developers can use this section to configure HTML error pages to be displayed to replace the error stack trace. <Customerrors mode = "remoteonly" defaultredirect = "genericerrorpage.htm"> <error statuscode = "403" Redirect = "noaccess.htm"/> <error statuscode = "404" Redirect = "deny "/> </customerrors> --> </system. web> </configuration>
Code details:
Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. htmlcontrols; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. windows. forms; using system. web. configuration; // <summary> // summary description for configurationoperator // </Summary> public class configurationoperator {Publ IC Enum configtype {// <summary> // the config file of the Asp.net website /// </Summary> webconfig = 1, /// <summary> /// config file of the Windows application /// </Summary> execonfig = 2} private configuration config; private string configpath; private configtype; /// <summary> /// constructor /// </Summary> /// <Param name = "configtype"> </param> Public configurationoperator (configtype) {This. configtype = config Type; If (configtype = configtype. execonfig) {configpath = application. executablepath;} else {configpath = httpcontext. current. request. applicationpath;} initialize ();} public configuration {get {return config;} set {Config = value ;}} /// <summary> /// constructor /// </Summary> /// <Param name = "path">. location of the config file </param> /// <Param name = "type">. the type of the config file, which can only be the website configuration file or application configuration file </P Aram> Public configurationoperator (string configpath, configtype) {This. configpath = configpath; this. configtype = configtype; initialize () ;}/// <summary> // instantiate the configuration. Depending on the type of the configuration file, different instantiation methods are adopted. // </Summary> private void initialize () {// if it is a configuration file of the winform application if (configtype = configtype. execonfig) {Config = system. configuration. configurationmanager. openexeconfiguration (confi Gpath);} else // webform configuration file {Config = system. web. configuration. webconfigurationmanager. openwebconfiguration (configpath) ;}/// <summary> /// add an application configuration node. If this node already exists, the value of the node is modified. // </Summary> /// <Param name = "key"> </param> /// <Param name = "value"> </param> Public void addreceivetting (string key, string Value) {paiettingssection paietting = (appsettingssection) config. getsection ("appsettings"); if (Ppsetting. settings [Key] = NULL) // if this node does not exist, add {deleetting. settings. add (Key, value);} else // if this node exists, modify {modifyappsetting (Key, value );}} /// <summary> /// Add a database connection string node. If this node already exists, the value of the node is modified. // </Summary> /// <Param name = "key"> </param> /// <Param name = "connectionstring"> </param> Public void addconnectionstring (string key, string connectionstring) {connectionstringssection connectionsetting = (Connectionstringssection) config. getsection ("connectionstrings"); If (connectionsetting. connectionstrings [Key] = NULL) // if this node does not exist, add {connectionstringsettings = new connectionstringsettings (Key, connectionstring); connectionsetting. connectionstrings. add (connectionstringsettings);} else // if this node exists, modify {modifyconnectionstring (Key, connectionstring) ;}/// <summary> // Add Database connection string node. If this node already exists, the value of the node is modified. // </Summary> /// <Param name = "name"> </param> /// <Param name = "duration"> </param> Public void addoutputcacheprofiles (string name, int duration) {// here the divergence is: modifying the remaining nodes is also similar to the operation outputcachesettingssection outputcachesetpartition = (outputcachesettingssection) config. getsection ("system. web/caching/outputcachesettings "); If (outputcachesettings. outputcacheprofiles [name] = NUL L) // if this node does not exist, add {outputcacheprofile outputcacheprofiles = new outputcacheprofile (name); outputcacheprofiles. duration = duration; outputcacheprofiles. location = system. web. UI. outputcachelocation. any; outputcacheprofiles. varybyparam = "*"; // currently, only one duration parameter is modified. Therefore, the method only has one parameter. To modify all parameters, expand outputcachesettings. outputcacheprofiles. add (outputcacheprofiles);} else // if this node exists, modify {modifyoutputcacheprofi Les (name, duration) ;}/// <summary> /// modify the application configuration node. If this node does not exist, the node and corresponding value are added. /// </Summary> /// <Param name = "name"> </param> /// <Param name = "newduration "> </param> Public void modifyoutputcacheprofiles (string name, int newduration) {outputcachesettingssection outputcachesettings = (outputcachesettingssection) config. getsection ("system. web/caching/outputcachesettings "); If (outputcachesettings. output Cacheprofiles [name]! = NULL) // if this node does not exist, add {outputcachesettings. outputcacheprofiles [name]. duration = newduration;} else // if this node does not exist, add {addoutputcacheprofiles (name, newduration) ;}/// <summary> // modify the application configuration node, if this node does not exist, the node and corresponding value are added. // </Summary> /// <Param name = "key"> node name </param> /// <Param name = "value"> node value </param> Public void modifyreceivetting (string key, string newvalue) {paiettingssection paietting = (Ett ETT Ingssection) config. getsection ("appsettings"); If (appsetting. settings [Key]! = NULL) // if this node exists, modify {deleetting. settings [Key]. value = newvalue;} else // if this node does not exist, add {addreceivetting (Key, newvalue) ;}/// <summary> // modify the database connection string node, if this node does not exist, the node and corresponding value are added. // </Summary> /// <Param name = "key"> node name </param> /// <Param name = "value"> node value </param> Public void modifyconnectionstring (string key, string connectionstring) {connectionstringssection connectionsetting = (connectionstringsse Ction) config. getsection ("connectionstrings"); If (connectionsetting. connectionstrings [Key]! = NULL) // if this node exists, modify {connectionsetting. connectionstrings [Key]. connectionstring = connectionstring;} else // if this node does not exist, add {addconnectionstring (Key, connectionstring );}} /// <summary> /// Save the modification /// </Summary> Public void save () {config. save ();}}
Call method:
Configurationoperator COP = new configurationoperator (configurationoperator. configtype. webconfig );
// Configurationoperator. configtype is an enumeration used to standardize the type of the modified config file. For details, see the definition in configurationoperator.
Cop. addoutputcacheprofiles ("webcache", 600 );
Cop. Save ();
Note: the permission of the web. config file. Otherwise, the system prompts that the permission is insufficient.
The appsonstrings code is from. Net2.0 Summary of the config file operation methods