内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

怎么在.NetMVC中讀寫配置文件-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)怎么在.Net MVC中讀寫配置文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)公司自成立以來,一直致力于為企業(yè)提供從網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、成都做網(wǎng)站、電子商務(wù)、網(wǎng)站推廣、網(wǎng)站優(yōu)化到為企業(yè)提供個(gè)性化軟件開發(fā)等基于互聯(lián)網(wǎng)的全面整合營(yíng)銷服務(wù)。公司擁有豐富的網(wǎng)站建設(shè)和互聯(lián)網(wǎng)應(yīng)用系統(tǒng)開發(fā)管理經(jīng)驗(yàn)、成熟的應(yīng)用系統(tǒng)解決方案、優(yōu)秀的網(wǎng)站開發(fā)工程師團(tuán)隊(duì)及專業(yè)的網(wǎng)站設(shè)計(jì)師團(tuán)隊(duì)。

我們可以在web.config的配置節(jié)寫入配置。


<configuration>
 <configSections>
 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 <!--這里可以定義配置節(jié) -->
 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
 </configSections>

但是把大量的配置都寫入這里也會(huì)造成web.config的臃腫,可以采用把配置節(jié)定義在這里,把具體配置信息保存在其他文件中。


以上傳配置信息為例,看一下理想的結(jié)構(gòu)

怎么在.Net MVC中讀寫配置文件

Config\Upload.config的內(nèi)容

怎么在.Net MVC中讀寫配置文件

1、配置的元素。采用<add />的形式,是一個(gè)鍵和值得表示形式。<add key="MaxSize" value="1500000" />。 
2、元素的集合,里面定義一系列的元素。<UploadConfig>……</UploadConfig> 
3、自定義節(jié),表示自定義的一個(gè)節(jié)點(diǎn)。<section name="UploadConfig"  type="Ninesky.Models.Config.UploadConfig, Ninesky.Models"/>


name:節(jié)點(diǎn)名,type:處理節(jié)點(diǎn)的類行,逗號(hào)前是類名,逗號(hào)后是縮在程序集。


我們要?jiǎng)?chuàng)建這個(gè)類來管理配置


一、創(chuàng)建鍵,值元素類。


里面只有兩個(gè)讀寫屬性key和value,內(nèi)容非常簡(jiǎn)單

using System.Configuration;

namespace Ninesky.Models.Config
{
 /// <summary>
 /// 鍵值元素類
 /// <remarks>
 /// 創(chuàng)建:2014.03.09
 /// </remarks>
 /// </summary>
 public class KeyValueElement:ConfigurationElement
 {
  /// <summary>
  /// 鍵
  /// </summary>
  [ConfigurationProperty("key",IsRequired=true)]
  public string Key {
   get { return this["key"].ToString(); }
   set { this["key"] = value; }
  }
  /// <summary>
  /// 值
  /// </summary>
  [ConfigurationProperty("value")]
  public string Value
  {
   get { return this["value"].ToString(); }
   set { this["value"] = value; }
  }
 }
}

二、創(chuàng)建元素集合類。內(nèi)容很簡(jiǎn)單都進(jìn)行了注釋

using System;
using System.Configuration;

namespace Ninesky.Models.Config
{
 /// <summary>
 /// 元素集合類
 /// <remarks>
 /// 創(chuàng)建:2014.03.09
 /// </remarks>
 /// </summary>
 [ConfigurationCollection(typeof(KeyValueElement))]
 public class KeyValueElementCollection:ConfigurationElementCollection
 {
  //忽略大小寫
  public KeyValueElementCollection() : base(StringComparer.OrdinalIgnoreCase) { }
  /// <summary>
  /// 集合中指定鍵的元素
  /// </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>
  /// 創(chuàng)建新元素.
  /// 必須重寫
  /// </summary>
  /// <returns></returns>
  protected override ConfigurationElement CreateNewElement()
  {
   return new KeyValueElement();
  }
  /// <summary>
  /// 獲取元素的鍵
  /// </summary>
  /// <param name="element"></param>
  /// <returns></returns>
  protected override object GetElementKey(ConfigurationElement element)
  {
   return ((KeyValueElement)element).Key;
  }
 }
}

三、配置節(jié)類


類中定義私有屬性KeyValues。進(jìn)行讀寫配置節(jié)集合,然后定義一系列的需要的配置屬性,操作就是對(duì)KeyValues的元素讀寫。

using System.Configuration;

namespace Ninesky.Models.Config
{
 /// <summary>
 /// 上傳設(shè)置配置節(jié)
 /// <remarks>
 /// 創(chuàng)建:2014.03.09
 /// </remarks>
 /// </summary>
 public class UploadConfig:ConfigurationSection
 {
  private static ConfigurationProperty _property = new ConfigurationProperty(string.Empty, typeof(KeyValueElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
  /// <summary>
  /// 配置列表
  /// </summary>
  [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
  private KeyValueElementCollection KeyValues
  {
   get { return (KeyValueElementCollection)base[_property]; }
   set { base[_property] = value; }
  }
  /// <summary>
  /// 較大大小
  /// </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>
  /// 上傳目錄
  /// 為應(yīng)用程序目錄起的文件夾名,前面不帶~/
  /// </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>
  /// 允許上傳的圖片類型文件擴(kuò)展,多個(gè)用“,”隔開
  /// </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>
  /// 允許上傳的Flash類型文件擴(kuò)展,多個(gè)用“,”隔開
  /// </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>
  /// 允許上傳的媒體類型文件擴(kuò)展,多個(gè)用“,”隔開
  /// </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>
  /// 允許上傳的文件類型文件擴(kuò)展,多個(gè)用“,”隔開
  /// </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;
   }
  }
 }
}

四、讀取配置 
也是最關(guān)系的,配置信息怎么讀出來。在上面類寫好后讀取非常容易了,只需用WebConfigurationManager.OpenWebConfiguration("~"). GetSection()獲取配置節(jié)的實(shí)例,局可以使用屬性值了

var _uploadConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").GetSection("UploadConfig") as Ninesky.Models.Config.UploadConfig;
   //文件較大限制
   int _maxSize = _uploadConfig.MaxSize;
   //文件路徑
   string _fileParth = _uploadConfig.Path ;
   string _dirName;
   //允許上傳的類型
   Hashtable extTable = new Hashtable();
   extTable.Add("image", _uploadConfig.ImageExt);
   extTable.Add("flash", _uploadConfig.FileExt);
   extTable.Add("media", _uploadConfig.MediaExt);
   extTable.Add("file", _uploadConfig.FileExt);

五、寫入配置
 跟讀取類似,把屬性值設(shè)置后save()一下,內(nèi)容就會(huì)保存到Config\Upload.config中。


代碼如下:

var _uploadConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").GetSection("UploadConfig") as Ninesky.Models.Config.UploadConfig;
   //文件較大限制
   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();

看完上述內(nèi)容,你們對(duì)怎么在.Net MVC中讀寫配置文件有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當(dāng)前名稱:怎么在.NetMVC中讀寫配置文件-創(chuàng)新互聯(lián)
文章地址:http://m.rwnh.cn/article32/iidsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、微信公眾號(hào)、自適應(yīng)網(wǎng)站、網(wǎng)站排名、標(biāo)簽優(yōu)化、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
禄丰县| 拉孜县| 江西省| 红原县| 英山县| 衡东县| 武平县| 新龙县| 托克逊县| 临沧市| 华蓥市| 吴江市| 绥滨县| 武定县| 福贡县| 论坛| 山西省| 海南省| 锡林郭勒盟| 博兴县| 宜都市| 陆河县| 彰化县| 濮阳县| 十堰市| 舞阳县| 甘孜| 米易县| 墨玉县| 南城县| 广州市| 灵璧县| 连州市| 绿春县| 丹阳市| 禹城市| 九寨沟县| 武胜县| 南溪县| 清丰县| 句容市|