ASP.NET MVC實現通用設置

来源:http://www.cnblogs.com/godbell/archive/2017/09/07/7492542.html
-Advertisement-
Play Games

網站中的設置實現方式有好幾種,其中有將設置類序列化然後保存到文件中(例如使用XML序列化然後以XML形式保存在文件中),或者將設置信息保存到資料庫中。 保存到資料庫中的方式就是將設置的項作為key,設置的值作為value,以key-value(鍵值對)的形式保存。 下麵使用保存到資料庫中的例子來說明 ...


網站中的設置實現方式有好幾種,其中有將設置類序列化然後保存到文件中(例如使用XML序列化然後以XML形式保存在文件中),或者將設置信息保存到資料庫中。

保存到資料庫中的方式就是將設置的項作為key,設置的值作為value,以key-value(鍵值對)的形式保存。

下麵使用保存到資料庫中的例子來說明,首先是設置信息資料庫表結構:

 

 Name是設置項,Value是設置值。對應的實體類如下:

public class Setting : ISettings
    {
        public int Id { get; set; }

        public string  Name { get; set; }

        public string Value { get; set; }
    }
View Code

這裡是使用Entity Framework,下麵是資料庫上下文實體類:

public partial class SettingContext : DbContext
    {
        public SettingContext():base("name=MyConnection")
        {
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer< SettingContext>(null);
            modelBuilder.Configurations.Add(new SettingMap());
            base.OnModelCreating(modelBuilder);
        }
      
        public IQueryable<Setting> Table
        {
            get
            {
                return Set<Setting>();
            }
        }
        public IQueryable<Setting> TableNoTracking
        {
            get
            {
                return Set<Setting>().AsNoTracking();
            }
        }
        public void Insert(Setting entity)
        {
            if (entity == null)
            {
                throw new ArgumentException("entity");
            }
            Set<Setting>().Add(entity);
            SaveChanges();
        }

        public void Update(Setting entity)
        {
            if (entity == null)
            {
                throw new ArgumentException("entity");
            }
            Set<Setting>().Attach(entity);
            Entry(entity).State = EntityState.Modified;
            SaveChanges();
        }
        /// <summary>
        /// 載入"設置"
        /// </summary>
        /// <typeparam name="T">設置必須實現介面ISettings</typeparam>
        /// <returns></returns>
        public T LoadSetting<T>() where T : ISettings, new()
        {
            //創建設置實例,然後再對其屬性賦值
            var setting = Activator.CreateInstance<T>();
            Type t = typeof(T);

            //獲取所有的屬性
            PropertyInfo[] props = t.GetProperties();
            //獲取資料庫中所有的設置
            var allSetting = TableNoTracking.ToList();
            if (allSetting!= null && allSetting.Count > 0)
            {
                foreach (PropertyInfo p in props)
                {
                    if (!p.CanRead || !p.CanWrite)
                    {
                        continue;
                    }
                    string key = t.Name + "." + p.Name;
                    key = key.Trim().ToLowerInvariant();    //轉換為小寫
                    var obj = allSetting.Where(s => s.Name == key).FirstOrDefault();
                    if (obj == null)
                    {
                        continue;
                    }
                    string valueStr = obj.Value;
                    //判斷是否可以轉換為
                    if (!TypeDescriptor.GetConverter(p.PropertyType).CanConvertFrom(typeof(string)))
                    {
                        continue;
                    }
                    if (!TypeDescriptor.GetConverter(p.PropertyType).IsValid(valueStr))
                    {
                        continue;
                    }
                    object value = TypeDescriptor.GetConverter(p.PropertyType).ConvertFromInvariantString(valueStr);
                    p.SetValue(setting, value);
                }
            }
            return setting;
        }
        /// <summary>
        /// 保存設置
        /// </summary>
        /// <typeparam name="T">設置必須實現介面ISettings</typeparam>
        /// <param name="setting"></param>
        public void SaveSetting<T>(T setting) where T : ISettings, new()
        {
            var allProperties = typeof(T).GetProperties();
            var allSettings = Table.ToList();
            foreach (PropertyInfo prop in allProperties)
            {
                if (!prop.CanRead || !prop.CanWrite)
                {
                    continue;
                }
                //判斷是否可以轉換
                if (!TypeDescriptor.GetConverter(prop.PropertyType).CanConvertFrom(typeof(string)))
                {
                    continue;
                }
                string key = typeof(T).Name + "." + prop.Name;
                key = key.Trim().ToLowerInvariant();
                dynamic value = prop.GetValue(setting, null);
                if (value == null)
                {
                    value = "";
                }
                var obj = allSettings.Where(s => s.Name == key).FirstOrDefault();
                //需要轉換為string
                string valueStr = TypeDescriptor.GetConverter(prop.PropertyType).ConvertToInvariantString(value);
                //已存在設置則更新,不存在則添加
                if (obj != null)
                {
                    obj.Value = valueStr;
                    Update(obj);
                }
                else
                {
                    obj = new Setting
                    {
                        Name = key,
                        Value = valueStr
                    };
                    Insert(obj);
                }
            }
        }
    }
View Code

由於需要用到泛型約束,所以要求設置類必須實現介面ISettings

EF映射類:

public class SettingMap : EntityTypeConfiguration<Setting>
    {
        public SettingMap()
        {
            this.ToTable("Setting");
            this.HasKey(s => s.Id);
            this.Property(s => s.Name).HasMaxLength(512);
        }
    }
View Code

基礎設置類:

/// <summary>
    /// 基礎設置
    /// </summary>
    public class BaseSetting : ISettings
    {
        [DisplayName("網站名稱")]
        public string SiteName { get; set; }
        [DisplayName("備案號")]
        public string SiteICP { get; set; }
        [DisplayName("聯繫方式")]
        public string SiteTel { get; set; }
        [DisplayName("版權信息")]
        public string Copyright { get; set; }
        [DisplayName("狀態")]
        public bool Status { get; set; }
        [DisplayName("緩存時間")]
        public int CacheTime { get; set; }
    }
View Code

在控制器中如下:

public class SettingController : Controller
    {
        SettingContext settingContext = new SettingContext();
        // GET: Setting
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Base()
        {
            var setting = settingContext.LoadSetting<BaseSetting>();
            return View(setting);
        }
        [HttpPost]
        public ActionResult Base(BaseSetting model)
        {
            if (ModelState.IsValid)
            {
                settingContext.SaveSetting<BaseSetting>(model);
            }
            return View(model);
        }
    }
View Code

視圖代碼(其實使用了Edit視圖基架):

 

@model SettingDemo.Models.BaseSetting

@{
    ViewBag.Title = "Base";
}

<h2>Base</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>BaseSetting</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.SiteName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SiteName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SiteName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SiteICP, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SiteICP, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SiteICP, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SiteTel, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SiteTel, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SiteTel, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Copyright, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Copyright, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Copyright, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Status)
                    @Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CacheTime, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CacheTime, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CacheTime, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
View Code

瀏覽前臺:

填寫設置,保存,查看資料庫

這裡設置項使用的是類名.屬性名,也可以使用完全名稱(完全命名空間.類名.屬性名),只需適當修改代碼即可。擴展自己的設置類需要類似BaseSetting那樣,實現介面ISettings。

參考資料:nopcommerce商城系統


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、分佈位置上的區別: kmalloc()和__get_free_pages()函數申請的記憶體位於物理記憶體的映射區域,而且在物理上也是連續的,它們與真實的物理地址只有一個固定的偏移,因此存在簡單的線性關係;(3G+896M)(低端記憶體); vmalloc函數申請的虛擬記憶體與物理記憶體之間也沒有簡單的換 ...
  • 添加樣式: 在html中,需要創建2層div來實現。一個div包含另一個div: 效果: ...
  • 表格控制項 Spread Studio 發佈了全新的 V11 CTP 版本。在此版本中,Spread For WinForms 引入了 Spread Common,也帶來了 Spread 性能的巨大提升和記憶體消耗的急劇下降。 ...
  • 1. 前言 做了WPF開發多年,一直未曾自己實現一個自定義Window Style,無論是《WPF編程寶典》或是各種博客都建議使用WindowStyle="None" 和 AllowsTransparency="True",於是想當然以為這樣就可以了。最近來了興緻想自己實現一個,才知道WindowS ...
  • Time Protocol(RFC-868)是一種非常簡單的應用層協議:它返回一個32位的二進位數字,這個數字描述了從1900年1月1日0時0分0秒到現在的秒數,伺服器在TCP的37號埠監聽時間協議請求。本函數將伺服器返回值轉化成本地時間。 先前不知道有現成的IPAddress.NetworkTo ...
  • 一、首先我們先講一下ref與out的區別和使用方法; 1、ref與out的區別: out:需要在使用前聲明變數,分配地址但不能賦值,但是需要在使用中的時候需要初始化(進入方法體中的時候需要先賦值在使用),至於為什麼要在方法體中使用,我個人認為是為了區別ref;(即只出不進) ref:需要在使用前聲明 ...
  • 請求過來,根據ip和埠,由iis伺服器進行接收,如果是靜態文件則直接返迴文件內容,如果無法解析,則根據映射規則找到對應請求尾碼 的ASPNET_ISAPI.dll處理程式集,交由其進行處理。 1.此時會生成IsapRuntime,其創建了WorkRequest對象, 2.接下來實例化HttpRun ...
  • HttpContext是ASP.NET中的核心對象,每一個請求都會創建一個對應的HttpContext對象,我們的應用程式便是通過HttpContext對象來獲取請求信息,最終生成響應,寫回到HttpContext中,完成一次請求處理。在前面幾章中也都有提到HttpContext,本章就來一起探索一 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...