SiteMap HtmlHelper ASP.NET MVC [英] SiteMap HtmlHelper ASP.NET MVC
问题描述
我已经重写了这篇文章,使其更加简单.这是我得到的代码(HtmlHelper
):
I've rewritten this post to make it more simple. This is the code I've got (a HtmlHelper
):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.CompilerServices;
using System.Web.Mvc;
using System.Text;
using System.Web.Routing;
namespace Intranet.Helpers
{
public static class MenuHelper
{
private static string GetBackLink(SiteMapNode parentNode)
{
return "<li class='li-back'><a href='" + parentNode.Url + "' title='" + parentNode.Title + "'></a></li>";
}
public static string Menu(this HtmlHelper helper)
{
var sb = new StringBuilder();
SiteMapNodeCollection siteMapNodeCollection;
sb.Append("<ul>");
SiteMapNode currentNode = SiteMap.CurrentNode;
if (!SiteMap.CurrentNode.Equals(SiteMap.RootNode))
{
if (!SiteMap.CurrentNode.HasChildNodes)
sb.Append(GetBackLink(SiteMap.CurrentNode.ParentNode.ParentNode));
else
sb.Append(GetBackLink(SiteMap.CurrentNode.ParentNode));
}
if (!SiteMap.CurrentNode.HasChildNodes)
siteMapNodeCollection = SiteMap.CurrentNode.ParentNode.ChildNodes;
else
siteMapNodeCollection = SiteMap.CurrentNode.ChildNodes;
foreach (SiteMapNode node in siteMapNodeCollection)
{
if(node.Description.Equals("hidden")) continue;
if (node.Url.Length == 0 && node.Description.Equals("separator"))
sb.Append("<li class=\"li-separator\"></li>");
else if (node.Url.Length == 0 && node.Description.Equals("heading"))
sb.Append("<li class=\"li-heading\">" + node.Title + "</li>");
else
{
if (node.HasChildNodes)
{
if (node.NextSibling != null)
sb.Append("<li class=\"li-sub\"><a href=\"" + node.Url + "\">" + node.Title + "</a></li>");
else
sb.Append("<li class=\"li-sub last-child\"><a href=\"" + node.Url + "\">" + node.Title + "</a></li>");
}
else
{
if (node.NextSibling != null)
sb.Append("<li><a href='" + node.Url + "'>" + node.Title + "</a></li>");
else
sb.Append("<li class='last-child'><a href='" + node.Url + "'>" + node.Title + "</a></li>");
}
}
}
sb.Append("</ul>");
return sb.ToString();
}
}
}
是此的变体.我正在使用 MVC区域库,所以我可以无法看到 MvcSiteMap 可以与此一起工作,因为{controller}/{action}
不再像以前那样工作了.
which is an altered version of this. I'm using MVC Areas Lib so I can't see how MvcSiteMap can work with this as it no longer works by {controller}/{action}
like it did before.
说我有一个类似http://localhost/mycontroller/myaction
的页面,它存在于SiteMap中,那么菜单将生成良好.但是说我http://localhost/mycontroller/myaction/50
并指定一个参数,SiteMap生成器将不再起作用,因为该URL不存在. 教程不涵盖
Say I have a page like http://localhost/mycontroller/myaction
and it exists in the SiteMap, then the menu will be generated fine. But say I do http://localhost/mycontroller/myaction/50
and specify a parameter, the SiteMap generator will no longer work because this URL does not exist. The tutorial doesn't cover MVC Areas Lib, so the solution to this problem doesn't work.
推荐答案
这篇关于SiteMap HtmlHelper ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!