MvcSiteMapProvider Menu with Filter Attributes in

2019-07-24 16:53发布

问题:

I am using this lib: https://github.com/maartenba/MvcSiteMapProvider

I want to have a menu structure like this:

<ul>
    <li><a href="/">Home</a></li>
    <li>
        <a href="#">Stuff</a>
        <ul>
            <li><a href="/Stuff/"<li>
            <li><a href="/Stuff/Add"<li>
        </ul>
    </li>
</ul>

I am using MvcSiteMapNode without xml; just with the decorator's way.

So I have this Controller:

[MvcSiteMapNode(Title = "Stuff", ParentKey = "root", Key = "stuff-key", Url = "#", ImageUrl = "fa-stuff")] 
public class StuffController : Controller {

    [MvcSiteMapNode(Title = "List", ParentKey = "stuff-key", Key = "stuff-list")]
    public ActionResult Index(){}

    [MvcSiteMapNode(Title = "Add", ParentKey = "stuff-key", Key = "stuff-add")]
    public ActionResult Add(){}
}

My problem is that when I GET the url /Stuff/ the index node is not selected and is only selected the parent node(stuff-key). Even when I GET /Stuff/Index happens the same.

回答1:

There are 2 problems here:

  1. There is a bug that you have just uncovered (thanks) which allows matching on route values when the URL is set explicitly or when the node is non-clickable.
  2. The character "#" is not technically allowed to be a URL. You can, but this will have the effect of going to "/#" (the home page), which is probably not what you wanted.

I am guessing for the second issue you wanted to make a node for grouping purposes, in which case it should be made non-clickable.

[MvcSiteMapNode(Title = "Stuff", ParentKey = "root", Key = "stuff-key", Clickable = false, ImageUrl = "fa-stuff")] 

This makes it generate plain text for "Stuff", not a hyperlink.

<ul id="menu">
    <li>
        <a href="/" title="Home">Home</a>
    </li>
    <li>
        Stuff
        <ul>
            <li>
                <a href="/Stuff" title="List">List</a>
            </li>
            <li>
                <a href="/Stuff/Add" title="Add">Add</a>
            </li>
        </ul>        
    </li>
</ul>

However, do note the matching problem won't go away until I get the patch released. So it won't work right until a version higher than 4.5.1 is available.