Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
740 views
in Technique[技术] by (71.8m points)

asp.net mvc - mvc sitemapprovider hash fragment

I'm using sitemapprovider to implement breadcrumbs in my MVC4 application. I need to add an hash fragment val

<mvcSiteMapNode title="Funds" controller="VcTransfer" action="Index/#"      preservedRouteParameters="">
 <mvcSiteMapNode title="Funds transfer" controller="VcTransfer" action="Index"  preservedRouteParameters="id" />
</mvcSiteMapNode>

I want the same result route as :

Redirect(Url.Action("Index", "VcTransfer") + "#Network-tab");

I hope I've been clear, thanks in advance!!

edit: the end result should appear like this:

(http:)//localhost:8080/VcTransfer/Index#Network-tab

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A fragment is processed by the browser but not posted back to the server, so it doesn't actually "count" when it comes to making the URL unique. Therefore, if you have multiple nodes that only differ by fragment, it would not be possible for MvcSiteMapProvider to tell them apart and it would always match the first node registered regardless of which one you select.

This basically means your breadcrumb won't change and the selected menu item won't change when an alternate fragment is selected. This isn't a bug, it is just not possible to do.

However, if you need to add the fragment for some other reason than navigation (javascript support for example), you can accomplish this by adding a custom attribute to the node and then modifying the node template to output the fragment in the view.

First, add a custom attribute to the node.

<mvcSiteMapNode title="Funds" controller="VcTransfer" action="Index" fragment="Network-tab">

Then make sure that you add the name of the custom attribute to as an attribute to ignore or it will appear in your URL as a query string parameter instead of a fragment.

Internal DI (root web.config):

<appSettings>
    <add key="MvcSiteMapProvider_AttributesToIgnore" value="fragment"/>
</appSettings>

External DI:

this.For<IReservedAttributeNameProvider>().Use<ReservedAttributeNameProvider>()
    .Ctor<IEnumerable<string>>("attributesToIgnore").Is(new string[] { "fragment" });

Then modify your /Views/Shared/DisplayTemplates/SiteMapNodeModel.cshtml file as shown below to output the fragment when it exists on the node.

@model MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models

@{
    var fragment = (Model.Attributes["fragment"] != null) ? "#" + Model.Attributes["fragment"] : "";
    var url = Model.Url + fragment;
}

@if (Model.IsCurrentNode && Model.SourceMetadata["HtmlHelper"].ToString() != "MvcSiteMapProvider.Web.Html.MenuHelper")  { 
    <text>@Model.Title</text>
} else if (Model.IsClickable) { 
    if (string.IsNullOrEmpty(Model.Description))
    {
        <a href="@url">@Model.Title</a>
    }
    else
    {
        <a href="@url" title="@Model.Description">@Model.Title</a>
    }
} else { 
    <text>@Model.Title</text>
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...