Portal Starter 源码深入剖析(一)
学ASP.net光看书看来是不行的,找一些经典的源代码来读读,对提升认识是很有帮助的。
在Microsoft的网站上找到几个范例,选择Portal是因为这个范例最大,可作为一个简单的门户站。
Portal的工作流程:
1、读取网站设置文件PortalCfg.xml至context中缓存起来,这个过程由Global.asax中的Application_BeginRequest()事件来完成的。
2、客户访问Portal站,执行Default.aspx,Default.aspx判断客户端是Mobile还是浏览器,如果是后者,引导客户至DesktopDefault.aspx
3、DesktopDefault.aspx完成网站各个栏目的展示以及各个栏目中相应模块的加载。
分析Global.asax中的Application_BeginRequest()事件:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
'默认访问主页
Dim tabIndex As Integer = 0
Dim tabId As Integer = 1
' Get TabIndex from querystring
If Not (Request.Params("tabindex") Is Nothing) Then
tabIndex = CInt(Request.Params("tabindex"))
End If
' Get TabID from querystring
If Not (Request.Params("tabid") Is Nothing) Then
tabId = CInt(Request.Params("tabid"))
End If
' Add the PortalSettings object to the context
' PortalSetting在Components\Configuration.vb中定义,其作用是根据传入的tabIndex,tabId加载相应栏目的元素。
' Context用来缓存相应栏目的设置。
Context.Items.Add("PortalSettings", New PortalSettings(tabIndex, tabId))
' Read the configuration info from the XML file or retrieve from Cache
' and add to the context
' Configuration类在Components\Configuration中定义,其作用是操作Portal网站的设置文件PortalCfg.xml文件。
' 同样是将设置文件缓存到Context中,这样网站的任何部分都可以访问到这些设置。
Dim config As Configuration = New Configuration()
Context.Items.Add("SiteSettings", config.GetSiteSettings())
Try
If Not (Request.UserLanguages Is Nothing) Then
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0))
' Default to English if there are no user languages
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 Try
End Sub
分析New PortalSettings(tabIndex, tabId),请看下面PortalSettings的构造函数:
Public Class PortalSettings
Public PortalId As Integer
Public PortalName As String
Public AlwaysShowEditButton As Boolean
Public DesktopTabs As New ArrayList()
Public MobileTabs As New ArrayList()
Public ActiveTab As New TabSettings()
......
Public Sub New(ByVal tabIndex As Integer, ByVal tabId As Integer)
' Get the configuration data
Dim config As Configuration = New Configuration()
' 通过Configuration类中的GetSiteSettings()将PortalCfg.xml文件导入到Context中,
' 随后通过返回值赋值给siteSettings,siteSettings被定义为一个增强型的Dataset类。
Dim siteSettings As SiteConfiguration = config.GetSiteSettings()
' Read the Desktop Tab Information, and sort by Tab Order
' 读取Tab信息,也就是Portal网站的栏目信息,放到DesktopTabs中。
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
' If the PortalSettings.ActiveTab property is set to 0, change it to
' the TabID of the first tab in the DesktopTabs collection
If Me.ActiveTab.TabId = 0 Then
Me.ActiveTab.TabId = CType(Me.DesktopTabs(0), TabStripDetails).TabId
End If
' Read the Mobile Tab Information, and sort by Tab Order
Dim mRow As SiteConfiguration.TabRow
For Each mRow In siteSettings.Tab.Select("ShowMobile='true'", "TabOrder")
Dim tabDetails As New TabStripDetails()
With tabdetails