ASP.NET MVC - Simple Breadcrumbs (SiteMap)

依然范特西╮ 提交于 2019-12-12 19:40:55

问题


I have developed a ASP.NET MVC 2 application and I want to put a simple breadcrumbs (sitemap) in each page like this:

Home > Movies > Details

It is equal the URL: http://localhost/home/movies/details

How can I achieve it? I would like to put it in my master page.

Thanks!


回答1:


If it is always equal to the URL, the absolute simplest thing would be to make use of that by using something like this:

var menuitems = Request.Url.AbsolutePath.Split("/".ToCharArray(),
    StringSplitOptions.RemoveEmptyEntries);

menuitems would now contain the menu items you need to perform a simple foreach loop and build your menu.




回答2:


I would recommend using MVCSiteMapProvider. It's available as a NuGet package.

It can be used to generate breadcrumbs (which you are probably asking about) and also site maps.

MvcSiteMapProvider is, as the name implies, an ASP.NET MVC SiteMapProvider implementation for the ASP.NET MVC framework. Targeted at ASP.NET MVC 2, it provides sitemap XML functionality and interoperability with the classic ASP.NET sitemap controls, like the SiteMapPath control for rendering breadcrumbs and the Menu control.

Based on areas, controller and action method names rather than hardcoded URL references, sitemap nodes are completely dynamic based on the routing engine used in an application. The dynamic character of ASP.NET MVC is followed in the MvcSiteMapProvider: there are numerous extensibility points that allow you to extend the basic functionality offered.



来源:https://stackoverflow.com/questions/5317238/asp-net-mvc-simple-breadcrumbs-sitemap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!