MVC sitemapprovider杂凑片段 [英] mvc sitemapprovider hash fragment

查看:296
本文介绍了MVC sitemapprovider杂凑片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用sitemapprovider来实现我的MVC4应用面包屑。
我需要添加一个哈希片段VAL

 < mvcSiteMapNode标题=资金控制器=VcTransfer行动=索引/#preservedRouteParameters =>
 < mvcSiteMapNode标题=资金转移控制器=VcTransfer行动=索引preservedRouteParameters =ID/>
< / mvcSiteMapNode>

我要的结果相同的路线为:

 重定向(Url.Action(指数,VcTransfer)+#网络选项卡);

我希望我已经清楚,在此先感谢!

编辑:最终的结果应该会出现这样的:

(HTTP:)//本地主机:8080 / VcTransfer /索引#网络标签


解决方案

一个片段是由浏览器处理而不回传到服务器,所以当它涉及到使URL独特的它实际上并没有数。因此,如果你有多个节点,只有片段不同,它不会是可能的MvcSiteMapProvider告诉他们分开,这将始终注册的第一个节点匹配,无论您选择其中之一。

这基本上意味着你的痕迹不会改变,当选择一个替代片段所选择的菜单项将不会改变。这是不是一个错误,它仅仅是不可能的事。

然而,如果你需要添加片段其他一些原因,而不是导航(例如JavaScript的支持),您可以通过在视图中添加自定义属性到节点,然后修改节点模板来输出片段做到这一点

首先,自定义属性添加到节点。

 < mvcSiteMapNode标题=资金控制器=VcTransfer行动=索引片段=网络标签>

然后确保你作为一个属性添加自定义属性的名称忽略,否则将出现在您的网址作为查询字符串参数,而不是一个片段。

内部DI(根web.config):

 <&的appSettings GT;
    <添加键=MvcSiteMapProvider_AttributesToIgnoreVALUE =片段/>
< /的appSettings>

外部DI:

<$p$p><$c$c>this.For<IReservedAttributeNameProvider>().Use<ReservedAttributeNameProvider>()
    .Ctor&LT;&IEnumerable的LT;串GT;方式&gt;(attributesToIgnore)是(新的String [] {片段});

然后修改 /Views/Shared/DisplayTemplates/SiteMapNodeModel.cshtml 文件,如下所示输出片段时存在的节点就可以了。

  @model MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models@ {
    VAR片段=(Model.Attributes [片段]!= NULL)? #+ Model.Attributes [片段]:;
    VAR URL = Model.Url +片段;
}@if(Model.IsCurrentNode&安培;&安培;!Model.SourceMetadata [的HtmlHelper]的ToString()=MvcSiteMapProvider.Web.Html.MenuHelper){
    &LT;文字和GT; @ Model.Title&LT; /文字和GT;
}否则如果(Model.IsClickable){
    如果(string.IsNullOrEmpty(Model.Description))
    {
        &LT; A HREF =@网址&GT; @ Model.Title&LT; / A&GT;
    }
    其他
    {
        &LT; A HREF =@网址称号=@ Model.Description&GT; @ Model.Title&LT; / A&GT;
    }
}其他{
    &LT;文字和GT; @ Model.Title&LT; /文字和GT;
}

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

解决方案

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

这篇关于MVC sitemapprovider杂凑片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆