C# 如何使用MvcSiteMapProvider从Mvc.sitemap获取节点的url
我想使用像“key”这样的属性从Mvc.sitemap文件中获取url?我想从助手那里打电话。我只是不知道如何使用mvcsitemap类从文件中检索节点来生成url。C# 如何使用MvcSiteMapProvider从Mvc.sitemap获取节点的url,c#,asp.net-mvc-3,mvcsitemapprovider,C#,Asp.net Mvc 3,Mvcsitemapprovider,我想使用像“key”这样的属性从Mvc.sitemap文件中获取url?我想从助手那里打电话。我只是不知道如何使用mvcsitemap类从文件中检索节点来生成url。 提前谢谢 2小时后,总共4小时,结果如下: public static class MyHelpers { private static Dictionary<string, object> SourceMetadata = new Dictionary<string, object> { { "H
提前谢谢 2小时后,总共4小时,结果如下:
public static class MyHelpers
{
private static Dictionary<string, object> SourceMetadata = new Dictionary<string, object> { { "HtmlHelper", typeof(MenuHelper).FullName } };
public static MvcSiteMapNode GetNodeByKey(this MvcSiteMapHtmlHelper helper, string nodeKey)
{
SiteMapNode node = helper.Provider.FindSiteMapNodeFromKey(nodeKey);
var mvcNode = node as MvcSiteMapNode;
return mvcNode;
}
}
public static MvcHtmlString MapLink(this MvcSiteMapHtmlHelper htmlHelper, string nodeKey)
{
return htmlHelper.MapLink(nodeKey, null, null);
}
public static MvcHtmlString MapLink(this MvcSiteMapHtmlHelper htmlHelper, string nodeKey, string linkText)
{
return htmlHelper.MapLink(nodeKey, linkText, null);
}
public static MvcHtmlString MapLink(this MvcSiteMapHtmlHelper htmlHelper, string nodeKey, string linkText, object htmlAttributes)
{
MvcSiteMapNode myNode = GetNodeByKey(htmlHelper, nodeKey);
//we build the a tag
TagBuilder builder = new TagBuilder("a");
// Add attributes
builder.MergeAttribute("href", myNode.Url);
if (htmlAttributes != null)
{
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
}
if (!string.IsNullOrWhiteSpace(linkText))
{
builder.InnerHtml = linkText;
}
else
{
builder.InnerHtml = myNode.Title;
}
string link = builder.ToString();
return MvcHtmlString.Create(link);
}