答案:图 1:浏览器中呈现的标准页眉和页脚示例
这是一个简单的示例,但您可以很容易地将它扩展,使其包含标准的页眉与导航,或者仅输出相应的 <!-- #include ---> 语句。请注意,如果希望页眉或页脚包含交互内容,应考虑使用 ASP.NET 用户控件。[SomePage.aspx 源代码 - 内容示例]
<FONT face="Arial" color="#cc66cc" size="5">常规页面内容</FONT>[Visual Basic Global.asax]
<%@ Application Language="VB" %><script runat="server">Sub Application_BeginRequest(sender As Object, e As EventArgs) ' 生成页眉 Context.Response.Write("<html>" + ControlChars.Lf + _"<body bgcolor=#efefef>" + ControlChars.Lf + "<hr>" + _ ControlChars.Lf) End Sub Sub Application_EndRequest(sender As Object, e As EventArgs) ' 生成页脚 Context.Response.Write("<hr>" + ControlChars.Lf + _ "2002 Microsoft Corporation 版权所有" + _ ControlChars.Lf + "</body>" + ControlChars.Lf + "</html>") End Sub </script>[C# Global.asax]
<%@ Application Language="C#" %><script runat="server">void Application_BeginRequest(Object sender, EventArgs e) { // 生成页眉 Context.Response.Write("<html>\n<body bgcolor=#efefef>\n<hr>\n"); } void Application_EndRequest(Object sender, EventArgs e) { // 生成页脚 Context.Response.Write("<hr>\2002 Microsoft Corporation 版权所有\n"); Context.Response.Write("</body>\n</html>"); }</script>如何在用户经过身份验证后显示欢迎信息?
回答:测试 User 上下文对象以查看用户是否经过身份验证。如果是,还要从 User 对象获取用户名。当然,这是本文开头的示例。[Visual Basic]
<script language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) { If User.Identity.IsAuthenticated Then welcome.Text = "欢迎" + User.Identity.Name Else ' 尚未登录,添加一个指向登录页的链接 welcome.Text = "请登录!" welcome.NavigateUrl = "signin.aspx" End If End Sub</script><asp:HyperLink id="welcome" runat="server" maintainstate="false"></asp:HyperLink>[C#]
<script language="C#" runat="server"> void Page_Load(object sender, EventArgs e) { if (User.Identity.IsAuthenticated) { welcome.Text = "欢迎" + User.Identity.Name; } else { // 尚未登录,添加一个指向登录页的链接 welcome.Text = "请登录!"; welcome.NavigateUrl = "signin.aspx"; } }</script><asp:HyperLink id="welcome" runat="server" maintainstate="false"></asp:HyperLink>Context.Items 简介
希望以上示例可以说明,使用手头仅有的上下文信息编写 Web 应用程序是多么容易。那么,如果可以用同样的方法访问您应用程序独有的一些上下文,不是很好吗?
这就是 Context.Items 集合的用途。它使用在参与处理请求的各部分代码中都可用的方法,保存应用程序的请求特有值。例如,同样一条信息可以用在 Global.asax、ASPX 页、页内的用户控件中,也可以由页调用的业务逻辑使用。
请考虑 IBuySpy Portal(英文)应用程序示例。它使用一个简单的主页 DesktopDefault.aspx 来显示门户内容。显示的内容取决于所选择的选项卡,以及用户(如果已经过身份验证)角色。
/article/Article/UploadFiles/200508/20050823184547101.gif
图 2:IbuySpy 主页
查询字符串包含正被请求的选项卡的 TabIndedx 和 TabId 参数。在处理请求的整个过程中,一直使用此信息筛选要显示给用户的数据。http://www.ibuyspyportal.com/DesktopDefault.aspx?tabindex=1&tabid=2(英文)
要使用查询字符串值,需要首先确保它是一个有效值,如果不是,则要进行一些错误处理。它并不是一大串代码,但是您真的要在每个使用该值的页和组件中复制它吗?当然不!在 Portal 示例中,甚至更多的地方都涉及到它,因为一旦我们知道了 TabId,就可以预先加载其他信息。
Portal 使用查询字符串值作为参数,以构造一个新的 PortalSettings 对象,并将它添加到 Global.asax 的 BeginRequest 事件的 Context.Items 中。由于在每个请求开始处都执行了开始请求,这使得与该选项卡有关的值在应用程序的所有页和组件中都可用。请求完成后,对象将被自动丢弃 - 非常整齐![Visual Basic Global.asax]
Sub Application_BeginRequest(sender As [Object], e As EventArgs) Dim tabIndex As Integer = 0 Dim tabId As Integer = 0 ' 从查询字符串获取 TabIndex If Not (Request.Params("tabindex") Is Nothing) Then tabIndex = Int32.Parse(Request.Params("tabindex")) End If ' 从查询字符串获取 TabID If Not (Request.Params("tabid") Is Nothing) Then tabId = Int32.Parse(Request.Params("tabid")) End If Context.Items.Add("PortalSettings", _New PortalSettings(tabIndex, tabId)) End Sub[C# Global.asax]
void Application_BeginRequest(Object sender, EventArgs e) { int tabIndex = 0; int tabId = 0; // 从查询字符串获取 TabIndex if (Request.Params["tabindex"] != null) {上一个:发送HTTP请求的两种方式
下一个:关于上下文-1 (转自msdn)