【问题标题】:MVC Sitemap CurrentNode is null and no breadcrumbMVC 站点地图 CurrentNode 为空且没有面包屑
【发布时间】:2014-03-14 14:30:39
【问题描述】:

当前节点为空,不渲染面包屑。站点地图文件如下:

<mvcSiteMapNode title="Reporting" iconClass="glyphicon glyphicon-stats" key="Reporting" area="Reporting" controller="Home" action="Index">
  <mvcSiteMapNode title="ReportFolder1" iconClass="glyphicon glyphicon-stats" key="ReportingReportFolder1" route="Reporting_Report" action="Report"> <!-- acts as a folder will just redirect to reporting home -->
    <mvcSiteMapNode title="Report1" description="abc" iconClass="glyphicon glyphicon-stats" key="Report1" route="Reporting_Report" action="Report" reportPath="/Reports/Report1"></mvcSiteMapNode>
  </mvcSiteMapNode>
  <mvcSiteMapNode title="ReportFolder2" iconClass="glyphicon glyphicon-stats" key="ReportingReportFolder2" route="Reporting_Report" action="Report"> <!-- acts as a folder  will just redirect to reporting home -->
    <mvcSiteMapNode title="Report2" description="abc" iconClass="glyphicon glyphicon-stats" key="Report2" route="Reporting_Report" action="Report" reportPath="/Reports/Report2"></mvcSiteMapNode>
  </mvcSiteMapNode>
</mvcSiteMapNode>

为此,我在该地区注册了一条特殊路线:

context.MapRoute(
    "Reporting_Report",
    "Reporting/Report/{*reportPath}",
    new { controller = "Home", action = "Report", reportPath = UrlParameter.Optional },
    new[] { "Web.Areas.Reporting.Controllers" }
);

context.MapRoute(
    "Reporting_default",
    "Reporting/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    new[] { "Web.Areas.Reporting.Controllers" }
);

控制器动作如下:

//
// GET: /Reporting/Report/
public ActionResult Report(string reportPath)
{
    if (String.IsNullOrWhiteSpace(reportPath))
        return RedirectToAction("Index");

    if (reportPath.StartsWith("/") == false)
        reportPath = "/" + reportPath;

    var viewModel = new HomeReportViewModel();
    viewModel.ReportPath = reportPath;

    return View(viewModel);
}

报告路径已成功传递给我的视图模型,一切正常。问题是站点地图节点不可用。 CurrentNode 为 null,并且未呈现面包屑。

节点标题“Reporting”的面包屑按原样呈现。

有什么建议吗?

【问题讨论】:

    标签: c# asp.net-mvc mvcsitemapprovider


    【解决方案1】:

    我怀疑这是因为您将“Reports/Report1”解析为路由中的 reportPath 参数。

    context.MapRoute(
        "Reporting_Report",
        "Reporting/Report/{*reportPath}",
        new { controller = "Home", action = "Report", reportPath = UrlParameter.Optional },
        new[] { "Web.Areas.Reporting.Controllers" }
    );
    

    在您的节点中,您将 reportPath 声明为“/Reports/Report1”(注意额外的正斜杠)。

    <mvcSiteMapNode title="Report1" description="abc" iconClass="glyphicon glyphicon-stats" key="Report1" route="Reporting_Report" action="Report" reportPath="/Reports/Report1"></mvcSiteMapNode>
    

    要匹配的参数,值必须完全匹配(除了不区分大小写的匹配)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      相关资源
      最近更新 更多