上篇随笔 所述的方法可以指定SiteMapPath控件使用指定目录,指定文件名的sitemap文件。但是如果遇到Transfer的页面或者带查询的页面参数时,内置的功能可能不能满足你的要求了,这时可以编写自已的Provider来实现,如下我编写一个JobXmlSiteMapProvider类,它继承于XmlSiteMapProvider类,其中须重写BuildSiteMap()方法,还有就是通过修改传入rawUrl来增强其适应Transfer的能力。 public class JobXmlSiteMapProvider : XmlSiteMapProvider { public override SiteMapNode BuildSiteMap() { return base.BuildSiteMap(); } public override SiteMapNode FindSiteMapNode(string rawUrl) { //针对Transfer页面进行优化 rawUrl = OptimizeTransferViewJob(rawUrl); rawUrl = OptimizeTransferViewCompany(rawUrl); return base.FindSiteMapNode(rawUrl); } public override SiteMapNode FindSiteMapNodeFromKey(string key) { return base.FindSiteMapNodeFromKey(key); } /**//// <summary> /// 针对Transfer ViewJob.aspx页面进行优化 /// </summary> private string OptimizeTransferViewJob(string rawUrl) { HttpContext context = HttpContext.Current; string urlToTransfer = context.Request.RawUrl; 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"; //string replace = "Con001_ProjectManage/Job/viewjob.aspx?id=$1"; //解决大小写问题 urlToTransfer = urlToTransfer.ToLower(); match = match.ToLower(); string matchExpression = Globals.GetApplicationPath() + match; Regex regEx = new Regex(matchExpression, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled); if (regEx.IsMatch(urlToTransfer)) { //rawUrl = Regex.Replace(urlToTransfer, match, replace); rawUrl = Globals.GetApplicationPath() + "/Con001_ProjectManage/Job/viewjob.aspx"; } return rawUrl; } /**//// <summary> /// 针对Transfer ViewCompany.aspx页面进行优化 /// </summary> private string OptimizeTransferViewCompany(string rawUrl) { HttpContext context = HttpContext.Current; string urlToTransfer = context.Request.RawUrl; 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"; //string replace = "Con001_ProjectManage/Job/viewcompany.aspx?id=$1"; //解决大小写问题 urlToTransfer = urlToTransfer.ToLower(); match = match.ToLower(); string matchExpression = Globals.GetApplicationPath() + match; Regex regEx = new Regex(matchExpression, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled); if (regEx.IsMatch(urlToTransfer)) { //如果请求路径符合正则表达式 //rawUrl = Regex.Replace(urlToTransfer, match, replace); rawUrl = Globals.GetApplicationPath() + "Con001_ProjectManage/Job/viewcompany.aspx"; } return rawUrl; } 另外还要修改一下Web.config文件,如: < add siteMapFile ="~/App_Data/Job.sitemap" name ="JobXmlSiteMapProvider" type ="CSDN.Job.Components.JobXmlSiteMapProvider, CSDN.Job.Components" /> 转载于:https://www.cnblogs.com/BillChen/archive/2006/03/08/345487.html