Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用MvcSiteMapProvider从Mvc.sitemap获取节点的url_C#_Asp.net Mvc 3_Mvcsitemapprovider - Fatal编程技术网

C# 如何使用MvcSiteMapProvider从Mvc.sitemap获取节点的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

我想使用像“key”这样的属性从Mvc.sitemap文件中获取url?我想从助手那里打电话。我只是不知道如何使用mvcsitemap类从文件中检索节点来生成url。
提前谢谢

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);
    }