根据ASP.NET MVC中的用户参数更改breadcrumb

时间:2015-07-21 14:00:20

标签: c# asp.net-mvc breadcrumbs mvcsitemapprovider

我使用MvcSiteMapProvider将Breadcrumbs添加到我的网站。 我想根据ActionResult中使用的参数更改痕迹路径。 所以,链接看起来像这样: RewriteEngine on RewriteCond %{REMOTE_ADDR} ^xxx\.xx\.xx\.[8-9]$ # your ip here RewriteCond %{REQUEST_URI} !^/index/ RewriteRule .? /index/ [R,L]

SiteMapPath的当前视图: localhost:49345/Evaluation/ChoicePeriod?typeControl=Input

预期观点: Main > Evaluation

路线:

Main > Evaluation > Input

网页:

  routes.MapRoute(
            name: "Evaluation",
            url: "Evaluation/ChoicePeriod/{action}/{typeControl}",
            defaults: new { action = "Index", typeControl = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Main", action = "Index", id = UrlParameter.Optional }
        );

我已将typeControl值添加到RouteValues,现在我不知道如何在SiteMapPath中显示它

<mvcSiteMapNode title="Main" controller="Main" action="Index">
 <mvcSiteMapNode title="Evaluation" area="Evaluation" controller="ChoicePeriod" action="Index" >
  <mvcSiteMapNode title="Input" area="Evaluation" controller="ChoicePeriod" action="Index" preservedRouteParameters="typeControl" key="typeControl"/>
 </mvcSiteMapNode>
</mvcSiteMapNode>

有没有办法让我的面包屑看起来像我期望的那样?感谢。

3 个答案:

答案 0 :(得分:0)

typeControl=""节点添加Evaluation参数,以便在有值时强制不匹配。

<mvcSiteMapNode title="Main" controller="Main" action="Index">
 <mvcSiteMapNode title="Evaluation" area="Evaluation" controller="ChoicePeriod" action="Index" typeControl="">
  <mvcSiteMapNode title="Input" area="Evaluation" controller="ChoicePeriod" action="Index" preservedRouteParameters="typeControl" key="typeControl"/>
 </mvcSiteMapNode>
</mvcSiteMapNode>

此外,您应该更新路由,以便生成与其匹配的相同网址,以便它提供controller RouteValue,因为MVC需要它。

routes.MapRoute(
    name: "Evaluation",
    url: "Evaluation/ChoicePeriod",
    defaults: new { controller = "ChoicePeriod", action = "Index" }
);

答案 1 :(得分:0)

您应该在mvc.sitemap中使用URL:

<mvcSiteMapNode title="Main" controller="Main" action="Index">
 <mvcSiteMapNode title="Evaluation" area="Evaluation" controller="ChoicePeriod" action="Index" >
  <mvcSiteMapNode title="Input" area="Evaluation" controller="ChoicePeriod" action="Index" url="/Evaluation/ChoicePeriod/Input"/>
 </mvcSiteMapNode>
</mvcSiteMapNode>

答案 2 :(得分:-1)

使用MvcBreadCrumbs,您可以添加如下控制器代码:

public class SampleController : Controller
{
    [BreadCrumb]
    public ActionResult GetProduct(int id)
    {
        var model = db.GetProduct(id);
        BreadCrumb.SetLabel("Product " + model.ProductName);
        return View(model);
    }
}

然后在View.cshtml上显示它:

@Html.Raw(BreadCrumb.Display())