增强XmlSiteMapProvider功能

上篇随笔 所述的方法可以指定SiteMapPath控件使用指定目录,指定文件名的sitemap文件。但是如果遇到Transfer的页面或者带查询的页面参数时,内置的功能可能不能满足你的要求了,这时可以编写自已的Provider来实现,如下我编写一个JobXmlSiteMapProvider类,它继承于XmlSiteMapProvider类,其中须重写BuildSiteMap()方法,还有就是通过修改传入rawUrl来增强其适应Transfer的能力。
None.gif      public   class  JobXmlSiteMapProvider : XmlSiteMapProvider
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public override SiteMapNode BuildSiteMap()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return base.BuildSiteMap();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override SiteMapNode FindSiteMapNode(string rawUrl)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{  
InBlock.gif            
//针对Transfer页面进行优化
InBlock.gif
            rawUrl = OptimizeTransferViewJob(rawUrl);
InBlock.gif            rawUrl 
= OptimizeTransferViewCompany(rawUrl);
InBlock.gif
InBlock.gif            
return base.FindSiteMapNode(rawUrl);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override SiteMapNode FindSiteMapNodeFromKey(string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return base.FindSiteMapNodeFromKey(key);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 针对Transfer ViewJob.aspx页面进行优化
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private string OptimizeTransferViewJob(string rawUrl)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpContext context 
= HttpContext.Current;
InBlock.gif            
string urlToTransfer = context.Request.RawUrl;
InBlock.gif            
string match = @"jobs\/([a-fA-F0-9]{32}|([a-fA-F0-9]{8})-([a-fA-F0-9]{4})-([a-fA-F0-9]{4})-([a-fA-F0-9]{4})-([a-fA-F0-9]{12}))\/viewjob.aspx";
InBlock.gif            
//string replace = "Con001_ProjectManage/Job/viewjob.aspx?id=$1";
InBlock.gif
InBlock.gif            
//解决大小写问题
InBlock.gif
            urlToTransfer = urlToTransfer.ToLower();
InBlock.gif            match 
= match.ToLower();
InBlock.gif
InBlock.gif            
string matchExpression = Globals.GetApplicationPath() + match;
InBlock.gif            Regex regEx 
= new Regex(matchExpression, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
InBlock.gif            
if (regEx.IsMatch(urlToTransfer))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//rawUrl = Regex.Replace(urlToTransfer, match, replace);
InBlock.gif
                rawUrl = Globals.GetApplicationPath() + "/Con001_ProjectManage/Job/viewjob.aspx";
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return rawUrl;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 针对Transfer ViewCompany.aspx页面进行优化
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private string OptimizeTransferViewCompany(string rawUrl)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpContext context 
= HttpContext.Current;
InBlock.gif            
string urlToTransfer = context.Request.RawUrl;
InBlock.gif            
string match = @"Jobs\/([a-fA-F0-9]{32}|([a-fA-F0-9]{8})-([a-fA-F0-9]{4})-([a-fA-F0-9]{4})-([a-fA-F0-9]{4})-([a-fA-F0-9]{12}))\/viewcompany.aspx";
InBlock.gif            
//string replace = "Con001_ProjectManage/Job/viewcompany.aspx?id=$1";
InBlock.gif
InBlock.gif            
//解决大小写问题
InBlock.gif
            urlToTransfer = urlToTransfer.ToLower();
InBlock.gif            match 
= match.ToLower();
InBlock.gif
InBlock.gif            
string matchExpression = Globals.GetApplicationPath() + match;
InBlock.gif            Regex regEx 
= new Regex(matchExpression, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
InBlock.gif            
if (regEx.IsMatch(urlToTransfer))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//如果请求路径符合正则表达式
InBlock.gif                
//rawUrl = Regex.Replace(urlToTransfer, match, replace);
InBlock.gif
                rawUrl = Globals.GetApplicationPath() + "Con001_ProjectManage/Job/viewcompany.aspx";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return rawUrl;
ExpandedSubBlockEnd.gif        }

另外还要修改一下Web.config文件,如:
None.gif         < add  siteMapFile ="~/App_Data/Job.sitemap"  name ="JobXmlSiteMapProvider"
None.gif            type
="CSDN.Job.Components.JobXmlSiteMapProvider, CSDN.Job.Components"   />

转载于:https://www.cnblogs.com/BillChen/archive/2006/03/08/345487.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值