Petshop3.0学习笔记(二)Global.asax文档分析

 

Global.asax文档和asp中的Global.asa文件的功能是一样的,都是用来宿主Application对象的事件的处理处理程序,然asp.net中的这个文件在内容上却和asp中的差别巨大,不可同日而语,再者在asp.netApplication对象和Session对象都是system.web命名空间的东东,确切的说应该是system.web.httpApplication类,它实现了一系列的接口和一系列的事件,非常强大,大家可以查看sdk文档。

下面我们来看看Petshop3.0中的Global.asax文档到底实现了什么:

using System;

using System.Web;

-------------------------------------

引用system.Web命名空间,我们所用到的大部分的对象如requestresponsecache等对象都在这个命名空间中

-----------------------------------------

using System.Diagnostics;

--------------------------------

应用程序跟踪,日志处理等功能的类在这个命名空间中

---------------------------------

using System.Configuration;

--------------------------

处理web.config文件时,用这个命名空间中的类

namespace PetShop.Web

{

     public class Global : HttpApplication   //Global继承自HttpApplication,可以应用所能应用到的所有的功能

     {

         // Read in the name of the event log to use from web.config

         private static string LOG_SOURCE = ConfigurationSettings.AppSettings["Event Log Source"];

//读取web.config文件中Event Log Source键值的值

         protected void Application_Error(object sender, EventArgs e)

         {

              // If an exception is thrown in the application then log it to an event log

              Exception x = Server.GetLastError().GetBaseException();

              EventLog.WriteEntry(LOG_SOURCE, x.ToString(), EventLogEntryType.Error);

         }

//上面的是一个全局的事件错误处理程序,把出现的错误写入到日志中

         /// <summary>

         /// Custom caching using the VaryByCustom attribute in the

         /// OuputCache directive.

         /// </summary>

         /// <param name="context">The current HTTP request</param>

         /// <param name="arg">Custom cache argument</param>

         /// <returns>Cache key</returns>

         public override string GetVaryByCustomString(HttpContext context, String arg) {

//重载HttpApplicationGetVaryByCustomString函数方法,返回值是一个字符串,功能是实现一个全局的根据当前请求context.Request中变量的不同生成不同的Cache的键值,以区分当前请求是否已经被验证,WebComponents.CleanString.InputText是应用程序中实现的一个处理表单输入字符串处理的类,我们可以在WebComponents文件加中看到CleanString这个类

              // cache key that is returned

              string cacheKey = "";

             

              switch(arg) {

 

                   // Custom caching for header control. We want to create two views

                   // of the header... one if the user is logged in and another if

                   // they are logged out.

                   // We us the forms authentication flag to check if the user is logged in or not

 

                   // Category page

                   case "CategoryPageKey":

 

                       if (Request.IsAuthenticated == true) {

                            cacheKey = "QQQ" + WebComponents.CleanString.InputText(context.Request.QueryString["categoryId"], 50) + WebComponents.CleanString.InputText(context.Request.QueryString["page"], 15);

                       }

                       else {

                            cacheKey = "AAA" + WebComponents.CleanString.InputText(context.Request.QueryString["categoryId"], 50) + WebComponents.CleanString.InputText(context.Request.QueryString["page"], 15);

                       }

 

                       break;

 

                   // Search page

                   case "SearchPageKey" :

 

                       if (Request.IsAuthenticated == true) {

                            cacheKey = "QQQ" + WebComponents.CleanString.InputText(context.Request.QueryString["keywords"], 50) + WebComponents.CleanString.InputText(context.Request["page"], 15);

                       }

                       else {

                            cacheKey = "AAA" + WebComponents.CleanString.InputText(context.Request.QueryString["keywords"], 50) + WebComponents.CleanString.InputText(context.Request["page"], 15);

                       }

 

                       break;

 

                   // Item page

                   case "ItemPageKey" :

 

                       if (Request.IsAuthenticated == true) {

                           cacheKey = "QQQ" + WebComponents.CleanString.InputText(context.Request.QueryString["productId"], 50) + WebComponents.CleanString.InputText(context.Request.QueryString["page"], 15);

                       }

                       else {

                           cacheKey = "AAA" + WebComponents.CleanString.InputText(context.Request.QueryString["productId"], 50) + WebComponents.CleanString.InputText(context.Request.QueryString["page"], 15);

                       }

 

                       break;

 

                   // Item details page

                   case "ItemDetailsPageKey" :

 

                       if (Request.IsAuthenticated == true) {

                            cacheKey = "QQQ" + WebComponents.CleanString.InputText(context.Request.QueryString["itemId"], 50);

                       }

                       else {

                            cacheKey = "AAA" + WebComponents.CleanString.InputText(context.Request.QueryString["itemId"], 50);

                       }

 

                       break;

 

 

                   // Used for pages such as help and default

                   case "UserId" :

 

                       if (Request.IsAuthenticated == true) {

                            cacheKey = "UserIdIn";

                       }

                       else {

                            cacheKey = "UserIdOut";

                       }

 

                       break;

              }

 

              return cacheKey;

         }

     }

}

总体上来说在petshop3.0这个文件中,主要是实现了一个处理全局错误的事件处理程序和一个功能是实现一个全局的根据当前请求context.Request中变量的不同生成不同的Cache的键值,以区分当前请求是否已经被验证的方法,但这个文件还可以生成更加强大的高级功能,在以后的学习中,多分析高手们的源代码,以期能够在自己的开发中应用。

posted @ 2005-11-06 13:25  torome  阅读(600)  评论(0编辑  收藏  举报