MVC sitemapcurrentnode是空的,没有面包屑
本文关键字:面包 sitemapcurrentnode MVC | 更新日期: 2023-09-27 17:50:13
当前节点为空,不呈现breadcrumb。Sitemap文件如下:
<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,则不呈现breadcrumb。
节点标题为"Reporting"的breadcrumb按原样呈现。
有什么建议吗?
我怀疑这是因为您将"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>
要匹配参数,值必须完全匹配(不区分大小写的匹配除外)