如何在MVC4中创建面包屑

时间:2015-01-14 06:05:00

标签: c# asp.net-mvc asp.net-mvc-4 sitemap web.sitemap

如何在不使用MVCsitemapprovider包的情况下在MVC4中创建面包屑。仅使用web.sitemap节点。

我已经尝试过这种方法,我想使用站点地图来获取父节点子节点并显示出来。

public static class BreadCrumbHelpers
{

    static StringBuilder breadcrumb = new StringBuilder();

    public static string BuildBreadcrumbNavigation(this HtmlHelper helper)
    {

       //remove breadcrumb on login page
       //if (helper.ViewContext.RouteData.Values["action"].ToString() == "Index" ||
       //    helper.ViewContext.RouteData.Values["controller"].ToString() == "Account")
       //{
       //    return string.Empty;
       //}

  //Area root
        if (helper.ViewContext.RouteData.DataTokens["area"] == "Admin")
        {

            MenuIndexStringBuilder(helper);
            //Return to previous breadcrumb
            if (helper.ViewContext.RouteData.Values["action"].ToString() == "Page2")
            {
                breadcrumb.Append("<li>" + helper.ActionLink("Page 1", "Page1", "Testing") + "</li>");
            }
        }       
        else if (helper.ViewContext.RouteData.DataTokens["area"] == "Student")
        {

            MenuIndexStringBuilder(helper);
            //Return to previous breadcrumb
            if (helper.ViewContext.RouteData.Values["action"].ToString() == "Page2")
            {
                breadcrumb.Append("<li>" + helper.ActionLink("Page 1", "Page1", "SPageTest") + "</li>");
            }
        }
        else
        {
            //Default page
            breadcrumb = new StringBuilder("<ul class=\"breadcrumb\"><li>")
                .Append(helper.ActionLink("Menu", "Index", "Template").ToHtmlString())
                .Append("</li>");

            //Return to previous breadcrumb
        }
        if (helper.ViewContext.RouteData.Values["action"].ToString() != "Index")

        {  
            BreadcrumbAppend(helper);
        }

        return breadcrumb.Append("</ul>").ToString();
    }

    private static void MenuIndexStringBuilder(this HtmlHelper helper)
    {
        breadcrumb = new StringBuilder("<ul class=\"breadcrumb\"><li>")
                  .Append(helper.ActionLink("Menu", "Index", "Template", new { area = "" }, null).ToHtmlString())
                  .Append("</li>");

    }
    private static void BreadcrumbAppend(this HtmlHelper helper)
    {
        breadcrumb.Append("<li>");
        breadcrumb.Append(helper.ActionLink(helper.ViewContext.RouteData.Values["action"].ToString().Titleize(),
                                            helper.ViewContext.RouteData.Values["action"].ToString(),
                                            helper.ViewContext.RouteData.Values["controller"].ToString()));
        breadcrumb.Append("</li>");

    }

}

0 个答案:

没有答案