Arm*_*est 9 sitemap mapping wildcard url-routing sitemapnode
我想在Web.SiteMap中匹配"近似"匹配
除了一件事之外,Web.Sitemap静态站点地图提供程序运行良好.这是静止的!
所以,如果我必须为我的页面上的每篇10,000篇文章都有一个sitemapnode,如下所示:
是否有某种通配符映射我可以使用SiteMap来匹配文章下的任何内容?
或者也许在我的Webforms页面中,有没有办法手动设置当前节点?
在使用ASP.Net MVC框架执行此操作时,我在此页面上找到了一些帮助,但仍在寻找Webforms的良好解决方案.
我认为我要做的是创建一个自定义的SiteMap Provider
这是对上述评论的回应.我无法发布完整的代码,但这基本上是我的提供商的工作方式.
假设您有一个页面article.aspx,它使用查询字符串参数"id"来检索和显示文章标题和正文.然后这是在Web.sitemap中:
<siteMapNode url="/article.aspx" title="(this will be replaced)" param="id" />
Run Code Online (Sandbox Code Playgroud)
然后,您创建此类:
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
<write your own code>
// override node link
HyperLink link = new HyperLink();
link.NavigateUrl = url + "?" + param + "=" + id.ToString();
link.Text = <the article title from the database>;
link.ToolTip = <the article title from the database>;
item.Controls.Add(link);
}
else
{
// if current node is a separator, initialize as usual
base.InitializeItem(item);
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后,您在代码中使用此提供程序就像使用静态提供程序一样.
<mycontrols:DynamicSiteMapPath ID="dsmpMain" runat="server" />
Run Code Online (Sandbox Code Playgroud)
我的班级比这更复杂,但这些是基础知识.您可以只分析您正在使用的友好URL,而不是使用查询字符串参数,而是使用它来检索正确的内容.要最小化每个请求的额外数据库查找,您可以向提供程序添加缓存机制(文章标题通常不会经常更改).
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
14384 次 |
| 最近记录: |