標籤:style blog http color os java io ar 檔案
本文主要介紹安裝頁面涉及到實現的功能以及原理。
初次啟動nopcommerce會進入一個URL為”/install”的頁面,這個頁面涉及到了2個功能:1.頁面全球化;2.資料準備工作。
0.儲備知識
特性
javascript事件
cookie
多活動結果集
1.頁面全球化1.1顯示地區文字
在安裝頁面右下角有一個選擇地區語言功能,可以看到裡面有相當多的語言可供選擇。選擇完對應地區後,頁面的文字也會相應變化。
頁面全球化有一種最簡單的實現就是分檔案夾建站,比如微軟的MSDN,但是同一個頁面共有的基礎設計恐怕非常冗餘——我是指最差的情況。
而的實現則是路由參數,比如。
nopcommerce的頁面是強型別視圖。開發人員在Nop.Web-InstallController-Index()裡定義好了視圖模型InstallModel然後往頁面輸出。
[Validator(typeof(InstallValidator))] public partial class InstallModel : BaseNopModel { public InstallModel() { this.AvailableLanguages = new List<SelectListItem>(); } [AllowHtml] public string AdminEmail { get; set; } [AllowHtml] [DataType(DataType.Password)] public string AdminPassword { get; set; } [AllowHtml] [DataType(DataType.Password)] public string ConfirmPassword { get; set; } [AllowHtml] public string DatabaseConnectionString { get; set; } public string DataProvider { get; set; } public bool DisableSqlCompact { get; set; } //SQL Server properties public string SqlConnectionInfo { get; set; } [AllowHtml] public string SqlServerName { get; set; } [AllowHtml] public string SqlDatabaseName { get; set; } [AllowHtml] public string SqlServerUsername { get; set; } [AllowHtml] public string SqlServerPassword { get; set; } public string SqlAuthenticationType { get; set; } public bool SqlServerCreateDatabase { get; set; } public bool UseCustomCollation { get; set; } [AllowHtml] public string Collation { get; set; } public bool DisableSampleDataOption { get; set; } public bool InstallSampleData { get; set; } public List<SelectListItem> AvailableLanguages { get; set; } }
這裡特性和繼承基類先不要管。大致說明一下就是,這個特性是視圖驗證架構FluentValidation用的,而父類是領域對象基類。
頁面設計地區地區的字串通過GetResource(string resourceName)方法統一產生。
/// <summary> /// Get locale resource value /// </summary> /// <param name="resourceName">Resource name</param> /// <returns>Resource value</returns> public string GetResource(string resourceName) { var language = GetCurrentLanguage(); if (language == null) return resourceName; var resourceValue = language.Resources .Where(r => r.Name.Equals(resourceName, StringComparison.InvariantCultureIgnoreCase)) .Select(r => r.Value) .FirstOrDefault(); if (String.IsNullOrEmpty(resourceValue)) //return name return resourceName; return resourceValue; }
這個方法的總流程(包括調用的子方法)是:
- 擷取“nop.installation.lang”的cookie值
- 遍曆並讀取“/App_Data/Localization/Installation/”目錄下符合命名要求(Regex)的XML檔案,將其中子節點資訊(頁面所用字串)儲存在InstallationLanguage對象中,將 根節點(<Language Name="簡體中文" IsDefault="false" IsRightToLeft="false">)儲存在List<InstallationLocaleResource> 中。
- 根據List<InstallationLocaleResource>中的Code屬性匹配cookie值,得出當前語言對象InstallationLanguage
- 如果cookie匹配不上(沒有cookie),則根據瀏覽器中Request.UserLanguages的喜好設定(httpContext.Request.UserLanguages.FirstOrDefault())匹配Code屬性
- 如果還是匹配不上,則返回IsDefault屬性為true的XML對應的InstallationLanguage
- 如果完全符合不上,則返回任意XML對應的InstallationLanguage
- 如果出什麼意外,GetCurrentLanguage返回null的話就直接輸出傳過來的字串
- 如果沒什麼意外的話就根據字串在 List<InstallationLocaleResource>匹配合格資訊並返回
以上操作涉及到的實作類別主要是InstallationLocalizationService。
而功能就完成了。
1.2變更地區文字
頁面輸出的下拉框是這樣子的:
nopcommerce的實現則是下拉框變更觸發onchange事件(onchange="window.location.href = this.value;"),繼而發出get請求頁面。
從結果上看頁面的路由沒有變化,但其實這是ChangeLanguage重新導向的結果:
public ActionResult ChangeLanguage(string language) { if (DataSettingsHelper.DatabaseIsInstalled()) return RedirectToRoute("HomePage"); _locService.SaveCurrentLanguage(language); //Reload the page return RedirectToAction("Index", "Install"); }
而中間的_locService.SaveCurrentLanguage(language);將名為“nop.installation.lang”的cookie的值變更為請求的語言類型。
public virtual void SaveCurrentLanguage(string languageCode) { var httpContext = EngineContext.Current.Resolve<HttpContextBase>(); var cookie = new HttpCookie(LanguageCookieName); cookie.HttpOnly = true; cookie.Value = languageCode; cookie.Expires = DateTime.Now.AddHours(24); httpContext.Response.Cookies.Remove(LanguageCookieName); httpContext.Response.Cookies.Add(cookie); }
因為cookie變更了,所以GetResource方法得到字串也會根據目前範圍變更。
第一句代碼正如其表述一般,是判斷資料庫已經安裝後,根據HomePage這個規則,重新導向到首頁。
2.資料準備工作
匯入資料相比就比較簡單。通過一系列判斷後執行多活動結果集(sql指令碼)。主要是由於強型別屬性比較多,所以其POST action if else 比較多。這些操作執行完後其實還有一個外掛程式安裝和許可權安裝的流程,自己看原始碼吧。
InstallController-public ActionResult Index(InstallModel model)
(002).NET大型B2C開源項目nopcommerce解析——安裝頁面