我只是将 MvcSiteMapProvider 从 v3 升级到 v4.6.3。
我看到升级说明表明:
一般来说,任何对的引用System.Web.SiteMap.Provider
都需要更新为MvcSiteMapProvider.SiteMaps.Current
我正在尝试使用以下方法获取站点地图节点:
SiteMaps.Current.FindSiteMapNode(rawUrl)
但它总是返回 null
我查看了代码。在站点地图中,它实际上是在调用该函数:
protected virtual ISiteMapNode FindSiteMapNodeFromUrlMatch(IUrlKey urlToMatch)
{
if (this.urlTable.ContainsKey(urlToMatch))
{
return this.urlTable[urlToMatch];
}
return null;
}
它试图在urlTable中找到匹配项。
我正在使用XmlSiteMapProvider
.
它定义var url = node.GetAttributeValue("url");
siteMapNode.Url = url;
siteMapNode.UrlResolver = node.GetAttributeValue("urlResolver");
因此,如果我没有在 .sitemap 文件中定义url
或属性。urlResolver
这些变量在生成节点时设置为空字符串。
当这个节点被传递给AddNode
函数时SiteMap
。
添加节点时
bool isMvcUrl = string.IsNullOrEmpty(node.UnresolvedUrl) && this.UsesDefaultUrlResolver(node);
此代码将检查是否存在url
或urlResolver
// Only store URLs if they are clickable and are configured using the Url
// property or provided by a custom URL resolver.
if (!isMvcUrl && node.Clickable)
{
url = this.siteMapChildStateFactory.CreateUrlKey(node);
// Check for duplicates (including matching or empty host names).
if (this.urlTable
.Where(k => string.Equals(k.Key.RootRelativeUrl, url.RootRelativeUrl, StringComparison.OrdinalIgnoreCase))
.Where(k => string.IsNullOrEmpty(k.Key.HostName) || string.IsNullOrEmpty(url.HostName) || string.Equals(k.Key.HostName, url.HostName, StringComparison.OrdinalIgnoreCase))
.Count() > 0)
{
var absoluteUrl = this.urlPath.ResolveUrl(node.UnresolvedUrl, string.IsNullOrEmpty(node.Protocol) ? Uri.UriSchemeHttp : node.Protocol, node.HostName);
throw new InvalidOperationException(string.Format(Resources.Messages.MultipleNodesWithIdenticalUrl, absoluteUrl));
}
}
// Add the URL
if (url != null)
{
this.urlTable[url] = node;
}
最后没有 url 添加到urlTable
,导致FindSiteMapNode
找不到任何东西。
我不确定是否需要特定的配置。或者我应该实施自定义XmlSiteMapProvider
只是添加网址。