作者:周世雄
2003 年 10 月
到了程式設計師「挖寶」的時刻了,美國微軟開發、免費、又開放原始程式碼的 Portal Starter Kit (入口網站入門套件) 中可抄的地方在那裡呢?可以拿來抄的寶 (地方) 包括:
入口網站引擎的動作原理如下:
整個入口網站的設定並沒有儲存到資料庫,而是儲存於 XML 檔 (PortalCfg.xml) 當中。
譬如於 [員工訊息] 頁籤於 PortalCfg.xml 的部份:
<Tab TabId="2" TabName="員工訊息" AccessRoles="All Users;" TabOrder="9" ShowMobile="true" MobileTabName="HR"> <Module ModuleId="9" ModuleTitle="員工聯絡紀錄" EditRoles="Admins;" ModuleDefId="2" PaneName="ContentPane" CacheTimeout="0" ModuleOrder="1" ShowMobile="true" /> <Module ModuleId="10" ModuleTitle="新員工教育手冊" EditRoles="Admins;" ModuleDefId="10" PaneName="ContentPane" CacheTimeout="0" ModuleOrder="3" ShowMobile="false" /> <Module ModuleId="43" ModuleTitle="拉拉山狗熊之窩" EditRoles="Admins;" ModuleDefId="6" PaneName="ContentPane" CacheTimeout="0" ModuleOrder="5" ShowMobile="false"> <Settings> <Setting Name="src">~/IMAGES/IMAGE19.JPG</Setting> <Setting Name="height">300</Setting> <Setting Name="width">400</Setting> </Settings> </Module> <Module ModuleId="44" ModuleTitle="旅遊景點" EditRoles="Admins;" ModuleDefId="8" PaneName="ContentPane" CacheTimeout="0" ModuleOrder="7" ShowMobile="false" /> </Tab>
則於 [員工訊息] 頁籤顯示如下圖:
譬如於 [管理員] 頁籤於PortalCfg.xml 的部份:
<Tab TabId="6" TabName="管理員" AccessRoles="Admins;" TabOrder="17" ShowMobile="false" MobileTabName="Admin"> <Module ModuleId="28" ModuleTitle="模組定義" EditRoles="Admins;" ModuleDefId="11" PaneName="RightPane" CacheTimeout="0" ModuleOrder="1" ShowMobile="false" /> <Module ModuleId="29" ModuleTitle="網站設定" EditRoles="Admins;" ModuleDefId="14" PaneName="ContentPane" CacheTimeout="0" ModuleOrder="1" ShowMobile="false" /> <Module ModuleId="30" ModuleTitle="頁籤 (Tab)" EditRoles="Admins;" ModuleDefId="13" PaneName="ContentPane" CacheTimeout="0" ModuleOrder="2" ShowMobile="false" /> <Module ModuleId="31" ModuleTitle="角色安全" EditRoles="Admins;" ModuleDefId="12" PaneName="ContentPane" CacheTimeout="0" ModuleOrder="3" ShowMobile="false" /> <Module ModuleId="32" ModuleTitle="管理使用者" EditRoles="Admins;" ModuleDefId="15" PaneName="ContentPane" CacheTimeout="0" ModuleOrder="4" ShowMobile="false" /> </Tab>
則於 [管理員] 頁籤顯示各個模組如下圖:
又譬如模組定義於 PortalCfg.xml 的部份:
<ModuleDefinition FriendlyName="公告" MobileSourceFile="MobileModules/Announcements.ascx" DesktopSourceFile="DesktopModules/Announcements.ascx" ModuleDefId="1" /> <ModuleDefinition FriendlyName="連絡人" MobileSourceFile="MobileModules/Contacts.ascx" DesktopSourceFile="DesktopModules/Contacts.ascx" ModuleDefId="2" /> <ModuleDefinition FriendlyName="討論區" MobileSourceFile="" DesktopSourceFile="DesktopModules /Discussion.ascx" ModuleDefId="3" />… <ModuleDefinition FriendlyName="網站設定 (Admin)" MobileSourceFile="" DesktopSourceFile="Admin /SiteSettings.ascx" ModuleDefId="14" /> <ModuleDefinition FriendlyName="管理使用者 (Admin)" MobileSourceFile="" DesktopSourceFile="Admin /Users.ascx" ModuleDefId="15" /> <ModuleDefinition FriendlyName="待處理" MobileSourceFile="DesktopModules/MyURL.ascx" DesktopSourceFile="DesktopModules/MyURL.ascx" ModuleDefId="16" />
則於 [管理員] 頁籤顯示如下圖:
入口網站引擎的動作原理的第一個步驟,為讀取入口網站 XML 設定檔到 Context 物件。Portal Starter Kit 採用一個特殊的技巧,使用 Context 物件暫存入口網站的設定,將入口網站的設定儲存於 XML 檔 (PortalCfg.xml) 暫存到 Context 物件。方法為 Global.asa 之 Application_BeginRequest() 副程式當中,將入口網站的 XML 檔設定存入到 Context 物件中後,同一個 Request 到的所有網站中的任何網頁、元件及控制項都可以藉由呼叫 Context 物件中的 PortalSettings 類別來讀取參數設定值。
Application_BeginRequest副程式:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) Dim tabIndex As Integer = 0 Dim tabId As Integer = 1 '取得頁籤索引 If Not (Request.Params("tabindex") Is Nothing) Then tabIndex = CInt(Request.Params("tabindex")) End If '取得頁籤代號 If Not (Request.Params("tabid") Is Nothing) Then tabId = CInt(Request.Params("tabid")) End If 'HttpContext.Current.Cache的cache(快取記憶體)儲存入口網站XML檔案設定檔, 同一個Request到的所有程式都 可以存取得到 '儲存PortalSettings設定(入口網站設定)到Context物件cache(快取記憶體), 以後可由 HttpContext.Current.Items("PortalSettings")取得PortalSettings設定(入口網站設定) Context.Items.Add("PortalSettings", New PortalSettings(tabIndex, tabId)) '儲存SiteSettings設定(模組設定)到Context物件cache(快取記憶體), 以後可由 HttpContext.Current.Items("SiteSettings")取得SiteSettings設定(模組設定) Dim config As Configuration = New Configuration() Context.Items.Add("SiteSettings", config.GetSiteSettings()) Try If Not (Request.UserLanguages Is Nothing) Then 'Request.UserLanguages(0) = "zh-tw" Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request. UserLanguages(0)) Else '若無使用者語言則預設為英語 Thread.CurrentThread.CurrentCulture = New CultureInfo("en-us") End If Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture Catch ex As Exception Thread.CurrentThread.CurrentCulture = New CultureInfo("en-us") End TryEnd Sub
其中「Dim config As Configuration = New Configuration()」將呼叫 \Components\Configuration.vb 的 New() 副程式:
Public Sub New(ByVal tabIndex As Integer, ByVal tabId As Integer) '取得設定資料 Dim config As Configuration = New Configuration() Dim siteSettings As SiteConfiguration = config.GetSiteSettings() '依照TabOrder排序依次讀取頁籤設定 Dim tRow As SiteConfiguration.TabRow For Each tRow In siteSettings.Tab.Select("", "TabOrder") Dim tabDetails As New TabStripDetails() With tabDetails .TabId = tRow.TabId .TabName = tRow.TabName .TabOrder = tRow.TabOrder .AuthorizedRoles = tRow.AccessRoles End With Me.DesktopTabs.Add(tabDetails) Next '若選取的頁籤索引(PortalSettings.ActiveTab)為0, 則改變為第一個TabID 'CType(Me.DesktopTabs(0), TabStripDetails).TabId = 1 If Me.ActiveTab.TabId = 0 Then Me.ActiveTab.TabId = CType(Me.DesktopTabs(0), TabStripDetails).TabId End If '依照TabOrder排序依次讀取行動裝置(Mobile)頁籤設定 Dim mRow As SiteConfiguration.TabRow For Each mRow In siteSettings.Tab.Select("ShowMobile='true'", "TabOrder") Dim tabDetails As New TabStripDetails() With tabdetails .TabId = mRow.TabId .TabName = mRow.MobileTabName .AuthorizedRoles = mRow.AccessRoles End With Me.MobileTabs.Add(tabDetails) Next '讀取選取頁籤(Active Tab)的模組資料(Module Information) Dim activeTab As SiteConfiguration.TabRow = siteSettings.Tab.FindByTabId(tabId) Dim moduleRow As SiteConfiguration._ModuleRow ' Get Modules for this Tab based on the Data Relation For Each moduleRow In activeTab.GetModuleRows() Dim moduleSettings As New moduleSettings() With moduleSettings .ModuleTitle = moduleRow.ModuleTitle .ModuleId = moduleRow.ModuleId .ModuleDefId = moduleRow.ModuleDefId .ModuleOrder = moduleRow.ModuleOrder .TabId = tabId .PaneName = moduleRow.PaneName .AuthorizedEditRoles = moduleRow.EditRoles .CacheTime = moduleRow.CacheTimeout .ShowMobile = moduleRow.ShowMobile '取得模組定義資料(ModuleDefinition data) Dim modDefRow As SiteConfiguration.ModuleDefinitionRow = siteSettings.ModuleDefinition.FindByModuleDefId(.ModuleDefId) 'URL .DesktopSrc = modDefRow.DesktopSourceFile .MobileSrc = modDefRow.MobileSourceFile End With Me.ActiveTab.Modules.Add(moduleSettings) Next '模組排序(依照ModuleOrder) Me.ActiveTab.Modules.Sort() '由第一行取得檔案的Global資料Dim globalSettings As SiteConfiguration.GlobalRow = siteSettings.Global.Rows(0) '讀取Portal global settings Me.PortalId = globalSettings.PortalId Me.PortalName = globalSettings.PortalName Me.AlwaysShowEditButton = globalSettings.AlwaysShowEditButton Me.ActiveTab.TabIndex = tabIndex Me.ActiveTab.TabId = tabId Me.ActiveTab.TabOrder = activeTab.TabOrder Me.ActiveTab.MobileTabName = activeTab.MobileTabName Me.ActiveTab.AuthorizedRoles = activeTab.AccessRoles Me.ActiveTab.TabName = activeTab.TabName Me.ActiveTab.ShowMobile = activeTab.ShowMobileEnd Sub
其中「config.GetSiteSettings()」將呼叫 \Components\Configuration.vb 的 GetSiteSettings() 函式,儲存入口網站 XML 檔案設定檔到快取記憶體 (cache)HttpContext.Current.Cache:
'取得入口網站XML檔案設定檔Public Function GetSiteSettings() As SiteConfiguration Dim siteSettings As SiteConfiguration = CType(HttpContext.Current.Cache("SiteSettings"), SiteConfiguration) '若SiteConfiguration未儲存於cache(快取記憶體), 則由XML檔案載入到cache. If siteSettings Is Nothing Then '產生dataset siteSettings = New SiteConfiguration() '取得XML設定檔位置 '於Web.Config讀取位置設定configFile() 'ConfigurationSettings.AppSettings("configFile") = "/PortalVBVS3/PortalCfg.xml" 'configFile = "C:\Program Files\ASP.NET Starter Kits\ASP.NET Portal (VBVS)\PortalVBVS3\PortalCfg.xml" Dim configFile As String = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings("configFile")) With siteSettings '設定AutoIncrement屬性為true以便容易加入資料 .Tab.TabIdColumn.AutoIncrement = True ._Module.ModuleIdColumn.AutoIncrement = True .ModuleDefinition.ModuleDefIdColumn.AutoIncrement = True '載入XML資料到DataSet siteSettings.ReadXml(configFile) End With '儲存dataset到cache HttpContext.Current.Cache.Insert("SiteSettings", siteSettings, New CacheDependency(configFile)) End If Return siteSettingsEnd Function
學會 Context 物件的技巧了嗎?
入口網站引擎的動作原理的第二個步驟,為顯示頁籤與各個模組。DesktopDefault.aspx 負責顯示頁籤與各個模組。動作原理如下:
執行 Global.asa 之 Application_BeginRequest 副程式後,將著執行 Default.aspx,非行動裝置導到 DesktopDefault.aspx:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '由Request.Browser("IsMobileDevice")瀏覽器種類, 來判斷使用者的電腦是否為行動裝置 If Request.Browser("IsMobileDevice") = "true" Then '若為行動裝置則重新導向到MobileDefault.aspx Response.Redirect("MobileDefault.aspx") Else '若為一般電腦(不是行動裝置)則重新導向到TimeEntry.aspx Response.Redirect("DesktopDefault.aspx") End IfEnd Sub
於DesktopDefault.aspx 讀取 Context 物件之入口網站設定以顯示頁籤與各個模組。
由 DesktopDefault.aspx 所包括的 DesktopPortalBanner.ascx 負責顯示頁籤,DesktopPortalBanner.ascx 的相關程式如下,檢查是否有權限瀏覽每一個頁籤後,資料繫結到頁籤 datalist:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '由Context物件(HttpContext.Current.Items("PortalSettings"))取得PortalSettings設定(入口網站設定) Dim _portalSettings As PortalSettings = CType(HttpContext.Current.Items("PortalSettings"), PortalSettings) '入口網站名稱, 存於XML檔<Global PortalId="0" PortalName="Portal Starter Kit 展示" siteName.Text = _portalSettings.PortalName '若已經登入則顯示歡迎詞 If Request.IsAuthenticated = True Then WelcomeMessage.Text = "您好 " & Context.User.Identity.Name & "! <" & "span class=Accent" & ">| <" & "/span" & ">" '若使用表單登入驗證方式則顯示登出連結 If Context.User.Identity.AuthenticationType = "Forms" Then LogoffLink = "<" & "span class=""Accent"">|</span>" & ControlChars.Cr & "<" & "a href=" & Global.GetApplicationPath(Request) & "/Admin/Logoff.aspx class=SiteLink> 登出" & "<" & "/a>" End If End If If ShowTabs = True Then tabIndex = _portalSettings.ActiveTab.TabIndex '顯示頁籤列表 Dim authorizedTabs As New ArrayList() Dim addedTabs As Integer = 0 Dim i As Integer For i = 0 To _portalSettings.DesktopTabs.Count - 1 Dim tab As TabStripDetails = CType(_portalSettings.DesktopTabs(i), TabStripDetails) '檢查是否有權限瀏覽每一個頁籤 If PortalSecurity.IsInRoles(tab.AuthorizedRoles) Then authorizedTabs.Add(tab) End If If addedTabs = tabIndex Then tabs.SelectedIndex = addedTabs End If addedTabs += 1 Next i '資料繫結頁籤 tabs.DataSource = authorizedTabs tabs.DataBind() End IfEnd Sub
Security.vb 中 IsInRole() 檢查是否有權限瀏覽每一個頁籤的相關程式,如下:
Public Shared Function IsInRole(ByVal role As String) As Boolean Return HttpContext.Current.User.IsInRole(role)End Function
接著 DesktopDefault.aspx 顯示各個模組,依照入口網站 XML 檔案設定,將此選取頁籤網頁的模組 (.ascx) 加到左邊、中間、右邊的版面,DesktopDefault.aspx 的相關程式如下:
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init '由Context物件(HttpContext.Current.Items("PortalSettings"))取得PortalSettings設定(入口網站設定) Dim _portalSettings As PortalSettings = CType(HttpContext.Current.Items("PortalSettings"), PortalSettings) '檢查訪客是否有瀏覽此選取頁籤網頁的權限, 若無權限則重新導向到AccessDenied.aspx, _portalSettings.ActiveTab.AuthorizedRoles = "All Users;"則表示有權限 If PortalSecurity.IsInRoles(_portalSettings.ActiveTab.AuthorizedRoles) = False Then Response.Redirect("~/Admin/AccessDenied.aspx") End If '若尚未登入驗證則動態插入一個登入模組(SignIn.ascx)到首頁左邊版面 If Request.IsAuthenticated = False And _portalSettings.ActiveTab.TabIndex = 0 Then LeftPane.Controls.Add(Page.LoadControl("~/DesktopModules/SignIn.ascx")) LeftPane.Visible = True End If '依照入口網站XML檔案設定, 將此選取頁籤網頁的模組加到左邊, 中間, 右邊的版面 If _portalSettings.ActiveTab.Modules.Count > 0 Then Dim _moduleSettings As ModuleSettings For Each _moduleSettings In _portalSettings.ActiveTab.Modules Dim parent As Control = Page.FindControl(_moduleSettings.PaneName) ' 若此模組未存於快取記憶體中則新建立user control instance, 並動態加入此網頁 If _moduleSettings.CacheTime = 0 Then Dim portalModule As PortalModuleControl = CType(Page.LoadControl (_moduleSettings.DesktopSrc), PortalModuleControl) portalModule.PortalId = _portalSettings.PortalId portalModule.ModuleConfiguration = _moduleSettings parent.Controls.Add(portalModule) Else Dim portalModule As New CachedPortalModuleControl() portalModule.PortalId = _portalSettings.PortalId portalModule.ModuleConfiguration = _moduleSettings parent.Controls.Add(portalModule) End If '模組間加一個空白行 parent.Controls.Add(New LiteralControl("<" + "br" + ">")) parent.Visible = True Next _moduleSettings End IfEnd Sub
好玩嗎?
Portal Starter Kit 英文版軟體可到以下網址免費下載與使用,包括原始碼:
http://www.asp.net/Default.aspx?tabindex=9&tabid=47
ASP.NET Starter Kit 中文版軟體可到「MSDN 下載專區」,分次免費下載與使用:
http://www.microsoft.com/taiwan/msdn/downloads/default.htm
或是透過 MSDN Subscription 附贈的 .NET 企業網站錦囊,一次取得所有中文版入門套件與架構白皮書,請參閱:
http://www.microsoft.com/taiwan/msdn/promo/
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=165122