濮阳杆衣贸易有限公司

主頁 > 知識庫 > .Net MVC網(wǎng)站中配置文件的讀寫

.Net MVC網(wǎng)站中配置文件的讀寫

熱門標簽:電銷外呼系統(tǒng)是違法的嗎 漯河外呼調(diào)研線路 電話機器人鑰匙扣 地圖標注位置怎么弄圖 電銷專用外呼線路 廣西房產(chǎn)智能外呼系統(tǒng)推薦 威力最大的電銷機器人 400電話唐山辦理 旅游地圖標注線路

網(wǎng)站中有很多需要設(shè)置的內(nèi)容,像網(wǎng)站信息,注冊設(shè)置,上傳設(shè)置等。如果保存在數(shù)據(jù)庫中需要單獨建張表,表中只有一條記錄,這樣會讓數(shù)據(jù)庫很臃腫,而且頻繁存取數(shù)據(jù)庫的效率也是個問題。而保存在config文件里是個不錯選擇,而且?guī)в芯彺婀δ埽?nbsp;

我們可以在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> 

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

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

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

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

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

我們要創(chuàng)建這個類來管理配置 

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

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

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)容很簡單都進行了注釋 

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。進行讀寫配置節(jié)集合,然后定義一系列的需要的配置屬性,操作就是對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>
  /// 允許上傳的圖片類型文件擴展,多個用“,”隔開
  /// /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類型文件擴展,多個用“,”隔開
  /// /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>
  /// 允許上傳的媒體類型文件擴展,多個用“,”隔開
  /// /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>
  /// 允許上傳的文件類型文件擴展,多個用“,”隔開
  /// /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é)的實例,局可以使用屬性值了 

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)容就會保存到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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • ASP.NET MVC5網(wǎng)站開發(fā)之用戶角色的后臺管理1(七)
  • ASP.NET MVC5網(wǎng)站開發(fā)之登錄、驗證和注銷管理員篇1(六)
  • ASP.NET MVC5網(wǎng)站開發(fā)之業(yè)務(wù)邏輯層的架構(gòu)和基本功能 (四)
  • ASP.NET MVC5網(wǎng)站開發(fā)之實現(xiàn)數(shù)據(jù)存儲層功能(三)
  • ASP.NET MVC5網(wǎng)站開發(fā)之網(wǎng)站設(shè)置(九)
  • ASP.NET MVC5網(wǎng)站開發(fā)修改及刪除文章(十)
  • ASP.NET MVC5網(wǎng)站開發(fā)添加文章(八)
  • ASP.NET MVC5網(wǎng)站開發(fā)文章管理架構(gòu)(七)
  • ASP.NET MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)
  • MVC4制作網(wǎng)站教程第一篇 緒論

標簽:銅陵 湖北 欽州 焦作 試駕邀約 綏化 湘西 無錫

巨人網(wǎng)絡(luò)通訊聲明:本文標題《.Net MVC網(wǎng)站中配置文件的讀寫》,本文關(guān)鍵詞  .Net,MVC,網(wǎng)站,中,配置文件,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《.Net MVC網(wǎng)站中配置文件的讀寫》相關(guān)的同類信息!
  • 本頁收集關(guān)于.Net MVC網(wǎng)站中配置文件的讀寫的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    沂南县| 张家界市| 扎兰屯市| 雅江县| 哈密市| 萨嘎县| 梁山县| 江山市| 洱源县| 新宁县| 凤山市| 麻城市| 安龙县| 绥棱县| 仪陇县| 沁水县| 远安县| 雷波县| 乌兰县| 平和县| 杂多县| 文成县| 宁国市| 准格尔旗| 乐业县| 宁强县| 江川县| 叙永县| 陆川县| 舟山市| 合水县| 凤台县| 蓝田县| 衡阳县| 北票市| 鄯善县| 贵溪市| 紫金县| 寻甸| 和静县| 凉山|