I would like to match "approximate" matches in Web.SiteMap
我想在Web.SiteMap中匹配“近似”匹配
The Web.Sitemap static sitemap provider works well, except for one thing. IT'S STATIC!
除了一件事之外,Web.Sitemap静态站点地图提供程序运行良好。这是静止的!
So, if I would have to have a sitemapnode for each of the 10,000 articles on my page like so :
所以,如果我必须为我页面上的每篇10,000篇文章都有一个sitemapnode,如下所示:
Is there some kind of Wildcard mapping I can do with the SiteMap to match Anything under Articles?
是否有某种通配符映射我可以使用SiteMap来匹配文章下的任何内容?
Or perhaps in my Webforms Page, is there a way to Manually set the current node?
或者也许在我的Webforms页面中,有没有办法手动设置当前节点?
I've found a "bit" of help on this page when doing this with the ASP.Net MVC Framework, but still looking for a good solution for Webforms.
在使用ASP.Net MVC框架执行此操作时,我在此页面上找到了一些帮助,但仍在为Webforms寻找一个好的解决方案。
I think what I'm going to have to do is create a custom SiteMap Provider
我认为我要做的是创建一个自定义的SiteMap Provider
7
This is in response to the comment above. I can't post the full code, but this is basically how my provider works.
这是对上述评论的回应。我无法发布完整的代码,但这基本上是我的提供商的工作方式。
Suppose you have a page article.aspx, and it uses query string parameter "id" to retrieve and display an article title and body. Then this is in Web.sitemap:
假设您有一个页面article.aspx,它使用查询字符串参数“id”来检索和显示文章标题和正文。然后这是在Web.sitemap中:
Then, you create this class:
然后,您创建此类:
public class DynamicSiteMapPath : SiteMapPath
{
protected override void InitializeItem(SiteMapNodeItem item)
{
if (item.ItemType != SiteMapNodeItemType.PathSeparator)
{
string url = item.SiteMapNode.Url;
string param = item.SiteMapNode["param"];
// get parameter value
int id = System.Web.HttpContext.Current.Request.QueryString[param];
// retrieve article from database using id
// override node link
HyperLink link = new HyperLink();
link.NavigateUrl = url + "?" + param + "=" + id.ToString();
link.Text = ;
link.ToolTip = ;
item.Controls.Add(link);
}
else
{
// if current node is a separator, initialize as usual
base.InitializeItem(item);
}
}
}
Finally, you use this provider in your code just like you would use the static provider.
最后,您在代码中使用此提供程序就像使用静态提供程序一样。
My class is more complicated than this, but these are the basics. Instead of using a querystring parameter, you could just analyze the friendly url you're using, and use that instead to retrieve the correct content. To minimize the additional db lookups with every request, you can add a caching mechanism to the provider (article title usually won't change often).
我的班级比这更复杂,但这些是基础知识。您可以只分析您正在使用的友好URL,而不是使用查询字符串参数,而是使用它来检索正确的内容。要最小化每个请求的额外数据库查找,您可以向提供程序添加缓存机制(文章标题通常不会经常更改)。
Hope this helps.
希望这可以帮助。
2
This is not entirely an answer to your question I think, but maybe it gives you an idea. I once wrote a DynamicSiteMapPath class inheriting SiteMapPath. I use a custom attribute in each
tag in Web.sitemap, like this:
这不完全是我认为的问题的答案,但也许它会给你一个想法。我曾经写过一个继承SiteMapPath的DynamicSiteMapPath类。我在Web.sitemap中的每个
Then the DynamicSiteMapPath class gets the "id" parameter value, retrieves the content from the database, and overrides the currently rendered sitemap item node with the correct title and link. It's gonna take a little work, but when done correctly this is a very neat way of providing dynamic page support.
然后,DynamicSiteMapPath类获取“id”参数值,从数据库中检索内容,并使用正确的标题和链接覆盖当前呈现的站点地图项节点。它需要一些工作,但如果正确完成,这是提供动态页面支持的一种非常简洁的方式。
2
I have been running into this problem and, frankly, not finding any solutions that made me happy... so I borrow ideas from here and there. My solution is multi-part: a) have the SiteMapProvider find the actual page handling the request and use it's node and b) use standard techniques for updating the sitemapnode from there.
我一直在遇到这个问题,坦率地说,没有找到让我开心的任何解决方案......所以我从这里和那里借鉴意见。我的解决方案是多部分:a)让SiteMapProvider找到处理请求的实际页面并使用它的节点,b)使用标准技术从那里更新sitemapnode。
A) The problem I kept running into is that if I didn't have the correct virtual path, the SiteMap.CurrentNode would be null and the fire the SiteMapResolve function. To solve this I subclassed XmlSiteMapProvider and overrode CurrentNode:
A)我一直遇到的问题是,如果我没有正确的虚拟路径,则SiteMap.CurrentNode将为null并触发SiteMapResolve函数。为了解决这个问题,我将XmlSiteMapProvider子类化并覆盖了CurrentNode:
namespace WebFormTools
{
class RouteBaseSitemapProvider : XmlSiteMapProvider
{
public override SiteMapNode CurrentNode
{
get
{
var node = base.CurrentNode;
if (node == null)
{
// we don't have a node, see if this is from a route
var page = HttpContext.Current.CurrentHandler as System.Web.UI.Page;
if (page != null && page.RouteData != null)
{
// try and get the Virtual path associated with this route
var handler = page.RouteData.RouteHandler as PageRouteHandler;
if (handler != null) {
// try and find that path instead.
node = FindSiteMapNode(handler.VirtualPath);
}
}
}
return node;
}
}
}
}
Basically if the default implementation doesn't find anything, look up the route (if any) and try to find the node using the handler's virtual path.
基本上,如果默认实现没有找到任何内容,请查找路由(如果有)并尝试使用处理程序的虚拟路径查找节点。
For reference here is part of my Web.Config, Global.asax and SiteMap files:
这里参考我的Web.Config,Global.asax和SiteMap文件的一部分:
Adding the provider
添加提供者
The route:
routes.MapPageRoute("EvalRoutes",
"Evals/{type}/New.aspx",
"~/Evals/New.aspx");
And the SiteMap:
和SiteMap:
B) I subclass System.Web.UI.Page, aptly named BaseClass, which adds a method to register handlers for the SiteMapResolve event:
B)我子类System.Web.UI.Page,恰当地命名为BaseClass,它添加了一个方法来注册SiteMapResolve事件的处理程序:
public System.Web.SiteMapNode Process(System.Web.SiteMapNode currentNode)
{
if (currentNode == null) return currentNode;
var page = HttpContext.Current.CurrentHandler as System.Web.UI.Page;
if (page != null && page.RouteData != null)
{
Dictionary replacements = new Dictionary();
// build a list of RegEx to aid in converstion, using RegEx so I can ignore class. Technically I could also
foreach (var key in page.RouteData.Values.Keys)
{
replacements.Add(new Regex(string.Format("\\{{{0}\\}}", key), RegexOptions.IgnoreCase), page.RouteData.Values[key].ToString());
}
// navigate up the nodes
var activeNode = currentNode;
while (activeNode != null)
{
// to the replacements
foreach(var replacement in replacements)
{
activeNode.Title = replacement.Key.Replace(activeNode.Title, replacement.Value);
}
activeNode = activeNode.ParentNode;
}
}
return currentNode;
}
I still need to have the URLs map appropriately (the would use the URL of the page receiving the route), which is sans routing information. I will probably use a custom Attribute in the sitemap to tell the node how to render the URL.
我仍然需要适当地映射URL(将使用接收路由的页面的URL),这是没有路由信息。我可能会在站点地图中使用自定义属性来告诉节点如何呈现URL。