Asp.net地址转义(分析)加强版--Dottext的地址分析模块的不足之处及相应的解决方案

(地址转义:指像http://www.cnblogs.com/default.aspx页面处理,default.aspx页面在URL中获得参数:BlogName = thinhunan。)

Dottext博客程序用IHttpHandler myhandler= PageParser.GetCompiledPageInstance (url,pagepath,context)来进行地址转义,这个方法返回的IHttpHandler的属性IsReusable有时会等于True,就是说返回的Handler会被Cache,我们在做Dottext的扩展时(如二级域名),这个机制不会出现什么问题,这是因为其实Dottext中很多种的URL都是最终给一个页面来处理,那就是能动态加载Control完成各种变化的Default.aspx页面,但如果有以下情况,问题就出来了:
1、http://thinhunan.sjjjyz.com/bloghttp://www.sjjyz.com/blog 要由不同的aspx文件,也就是不同的类来处理时
2、http://thinhunan.sjjyz.com/blog/2005/10/27/234.html 和 http://thinhunan.sjjyz.com/blog/2005/10/27/234.html?comment=1 要由不同的类来处理时
3、同是http://thinhunan.sjjjyz.com/blog ,但要根据客户端是IE还是手机由不同的类处理时

问题就出来了,因为Caching在缓存IHttpHandler时,Key根据的是Request的Path(或者FilePath)信息,就是说,不管是上面的第一种还是第二种情况,IHttpHandler会错误的看成是可以重用缓存中的前一次有细微不同的URL生成的IHttpHandler,而第三种情况,在我们的IHttpHandlerFactory类中,判断客户端的动作也不会执行,因为HttpModules会在HttpHandlers之前进行动作,而在Machine.config文件中

        <httpModules>
            
<add
                
name="OutputCache"
                type
="System.Web.Caching.OutputCacheModule" />
……        
</httpModules>

Cache是第一个HttpModule,所以,如果第一次是用户用手机访问了你的首页,那以后用IE来访问的IE都会被送上一堆不能解析的WML代码。

由此可见,PageParser.GetCompilePageInstance()方法也许并不能满足我们的需求。那怎么办呢?
对System.Web.UI进行改进和扩展?这是行不通的,因为其中大部分类、方法都是internal的,不同Assembly不能访问。

可行的办法是:其实System.Web.UI.Page本身继承了IHttpHandler,而且它本身的IsReusable返回的是false,既然如此,那我们干脆只接用反射返回页面实例就行了,实现代码如下:

        private static Assembly _dotTextWebAssembly = null;
        
/// <summary>
        
/// Assembly
        
/// </summary>

        private static Assembly DotTextWebAssembly
        
{
            
get
            
{
                
if(_dotTextWebAssembly == null)
                
{
                    _dotTextWebAssembly 
= (Assembly)HttpRuntime.Cache["WebDllPath"];
                    
if(_dotTextWebAssembly == null)
                    
{
                        AssemblyName an 
= new AssemblyName();
                        an.Name 
= "Dottext.Web";
                        _dotTextWebAssembly 
= AppDomain.CurrentDomain.Load(an);
                        HttpRuntime.Cache.Insert(
"WebDllPath",_dotTextWebAssembly,null,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromDays(1),System.Web.Caching.CacheItemPriority.NotRemovable,null);
    
                    }

                }

                
                
return _dotTextWebAssembly;
            }

        }

        
private static IHttpHandler GetWapHandlerByPage(string pageName)
        
{
            IHttpHandler waphandler 
= null;
            
//waphandler = (IHttpHandler)HttpRuntime.Cache["WapHandler_"+pageName]; 此处不能用Cache,因为Handler的IsReUsable为false;
            
//if(waphandler == null)
            
//{
                Type type = DotTextWebAssembly.GetType("Dottext.Web."+pageName.Replace(".aspx",""),true,true);
                waphandler 
= (IHttpHandler)System.Activator.CreateInstance(type);
                
//HttpRuntime.Cache.Insert("WapHandler_"+pageName,waphandler,null,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromHours(1),System.Web.Caching.CacheItemPriority.NotRemovable,null);
            
//}
            return waphandler;
        }


-----------------------------------------
原创文章,转载请注明出处

posted @ 2005-11-03 15:55  Think  阅读(4952)  评论(15编辑  收藏  举报