. Net MVC website configuration file read/write,. netmvc
The website contains many content that needs to be configured, such as website information, registration settings, and upload settings. If you need to create a separate table to store data in the database, there is only one record in the table, which will make the database very bloated and the efficiency of frequent database access is also a problem. Saving in the config file is a good choice and has the cache function!
We can write the configuration in the configuration section of web. config.
<Configuration> <configSections> <section name = "entityFramework" type = "System. data. entity. internal. configFile. entityFrameworkSection, EntityFramework, Version = 6.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 "requirePermission =" false "/> <! -- The configuration section can be defined here --> <! -- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink? LinkID = 237468 --> </configSections>
However, writing a large number of configurations here will also result in a bloated web. config. You can define the configuration section here and save the specific configuration information in other files.
The preceding configuration information is used as an example to describe the ideal structure.
Contents of Config \ Upload. config
1. configuration element. In the form of <add/>, it is a key and a value representation. <Add key = "MaxSize" value = "1500000"/>.
2. A collection of elements, which define a series of elements. <UploadConfig> ...... </UploadConfig>
3. Custom section, indicating a custom node. <Section name = "UploadConfig" type = "Ninesky. Models. Config. UploadConfig, Ninesky. Models"/>
Name: node name, type: class line of the processing node. The class name is before the comma, and the class name is after the comma in the Assembly.
We want to create this class to manage the configuration.
1. Create a key and value element class.
There are only two read/write attributes, key and value. The content is very simple.
Using System. configuration; namespace Ninesky. models. config {/// <summary> /// key-value element class /// <remarks> /// create: 2014.03.09 // </remarks> /// </summary> public class KeyValueElement: configurationElement {// <summary> /// key /// </summary> [ConfigurationProperty ("key", IsRequired = true)] public string Key {get {return this ["key"]. toString () ;}set {this ["key"] = value ;}} /// <summary> /// value /// </summary> [ConfigurationProperty ("Value")] public string value {get {return this ["value"]. toString () ;}set {this ["value"] = value ;}}}}
2. Create an element collection class.The content is simple and commented out.
Using System; using System. configuration; namespace Ninesky. models. config {/// <summary> /// element collection class /// <remarks> /// create: 2014.03.09 // </remarks> /// </summary> [ConfigurationCollection (typeof (KeyValueElement)] public class KeyValueElementCollection: ConfigurationElementCollection {// ignore case-insensitive public KeyValueElementCollection (): base (StringComparer. ordinalIgnoreCase) {}/// <summary> /// element of the specified key in the Set /// </summary> /// <param name = "name"> </param>/ // <returns> </returns> new public KeyValueElement this [string name] {get {return (KeyValueElement) base. baseGet (name);} set {if (base. properties. contains (name) base [name] = value; else base. baseAdd (value) ;}/// <summary> // create a new element. /// rewrite required /// </summary> /// <returns> </returns> protected override ConfigurationElement CreateNewElement () {return new KeyValueElement ();} /// <summary> /// obtain the key of the element /// </summary> /// <param name = "element"> </param> /// <returns> </returns> protected override object GetElementKey (ConfigurationElement element) {return (KeyValueElement) element ). key ;}}}
Iii. Configuration section
Class defines the private property KeyValues. Read/write configuration section set, and then define a series of required configuration attributes. The operation is to read and write the elements of KeyValues.
Using System. configuration; namespace Ninesky. models. config {/// <summary> /// upload settings configuration section /// <remarks> /// create: 2014.03.09 // </remarks> /// </summary> public class UploadConfig: ConfigurationSection {private static ConfigurationProperty _ property = new ConfigurationProperty (string. empty, typeof (KeyValueElementCollection), null, ConfigurationPropertyOptions. isDefaultCollection); // <summary> // configuration list /// </Summary> [ConfigurationProperty ("", Options = ConfigurationPropertyOptions. isDefaultCollection)] private KeyValueElementCollection KeyValues {get {return (KeyValueElementCollection) base [_ property] ;}set {base [_ property] = value ;}} /// <summary> /// maximum size /// </summary> public int MaxSize {get {int _ value = 0; if (KeyValues ["MaxSize"]! = Null) int. tryParse (KeyValues ["MaxSize"]. value, out _ value); return _ value;} set {if (KeyValues ["MaxSize"] = null) KeyValues ["MaxSize"] = new KeyValueElement () {Key = "MaxSize", Value = value. toString ()}; else KeyValues ["MaxSize"]. value = value. toString () ;}/// <summary> /// upload directory /// name of the folder from the application directory, not prefixed ~ //// </Summary> public string Path {get {if (KeyValues ["Path"] = null) return "Upload"; return KeyValues ["Path"]. value;} set {if (KeyValues ["Path"] = null) KeyValues ["Path"] = new KeyValueElement () {Key = "Path", Value = value }; else KeyValues ["Path"]. value = value ;}/// <summary> /// extended image type files that can be uploaded. for multiple file types, use ", "separated /// </summary> public string ImageExt {get {if (KeyValues [" ImageExt "] = null) return string. empty; return KeyValues ["ImageExt"]. value;} set {if (KeyValues ["ImageExt"] = null) KeyValues ["ImageExt"] = new KeyValueElement () {Key = "ImageExt", Value = value }; else KeyValues ["ImageExt"]. value = value ;}/// <summary> // extended Flash files that can be uploaded. "separated /// </summary> public string FlashExt {get {if (KeyValues [" FlashExt "] = null) return string. empty; return KeyValues ["FlashExt"]. value;} set {if (KeyValues ["FlashExt"] = null) KeyValues ["FlashExt"] = new KeyValueElement () {Key = "FlashExt", Value = value }; else KeyValues ["FlashExt"]. value = value ;}//< summary> /// you can specify the number of media files that can be uploaded. "separated /// </summary> public string MediaExt {get {if (KeyValues [" MediaExt "] = null) return string. empty; return KeyValues ["MediaExt"]. value;} set {if (KeyValues ["MediaExt"] = null) KeyValues ["MediaExt"] = new KeyValueElement () {Key = "MediaExt", Value = value }; else KeyValues ["MediaExt"]. value = value ;}/// <summary> // extended file type files that can be uploaded. "separated /// </summary> public string FileExt {get {if (KeyValues [" FileExt "] = null) return string. empty; return KeyValues ["FileExt"]. value;} set {if (KeyValues ["FileExt"] = null) KeyValues ["FileExt"] = new KeyValueElement () {Key = "FileExt", Value = value }; else KeyValues ["FileExt"]. value = value ;}}}}
Iv. Read configurations
And how to read the configuration information. It is very easy to read the above class. You only need to use WebConfigurationManager. OpenWebConfiguration ("~ "). GetSection () gets the instance of the configuration section. You can use the attribute value.
Var _ uploadConfig = System. Web. Configuration. WebConfigurationManager. OpenWebConfiguration ("~ "). GetSection ("UploadConfig") as Ninesky. models. config. uploadConfig; // maximum file size: int _ maxSize = _ uploadConfig. maxSize; // file path string _ fileParth = _ uploadConfig. path; string _ dirName; // the type that can be uploaded. Hashtable extTable = new Hashtable (); extTable. add ("image", _ uploadConfig. imageExt); extTable. add ("flash", _ uploadConfig. fileExt); extTable. add ("media", _ uploadConfig. mediaExt); extTable. add ("file", _ uploadConfig. fileExt );
5. Write Configuration
Similar to reading, after setting the attribute value and saving (), the content will be saved to Config \ Upload. config.
The Code is as follows:
Var _ uploadConfig = System. Web. Configuration. WebConfigurationManager. OpenWebConfiguration ("~ "). GetSection ("UploadConfig") as Ninesky. models. config. uploadConfig; // maximum file size: int _ maxSize = _ uploadConfig. maxSize; _ uploadConfig. fileExt = "doc, docx, xls, xlsx, ppt, htm, html, txt, zip, rar, gz, bz2"; _ uploadConfig. flashExt = "swf, flv"; _ uploadConfig. imageExt = "gif, jpg, jpeg, png, bmp"; _ uploadConfig. mediaExt = "swf, flv, mp3, wav, wma, wmv, mid, avi, mpg, asf, rm, rmvb"; _ uploadConfig. path = "Upload"; _ uploadConfig. currentConfiguration. save ();
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.