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文件中

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

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

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

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

None.gif          private   static  Assembly _dotTextWebAssembly  =   null ;
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// Assembly
ExpandedBlockEnd.gif        
/// </summary>

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

ExpandedSubBlockEnd.gif                }

InBlock.gif                
InBlock.gif                
return _dotTextWebAssembly;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值