Simple Breadcrumbs in Razor

If you wish to add Breadcrumb navigation to your site. This is a quick and easy way to do it.  This is only effective if every controller has an Index view and if you are passing route variables such as 'acct=xxx' then this will not accommodate those values so you have to make sure your app is able to manage without the variables and offer another option.

@Html.ActionLink("Home", "Index", "Home")

@if(ViewContext.RouteData.Values["controller"].ToString() != "Home") {

@Html.ActionLink(ViewContext.RouteData.Values["controller"].ToString(), "Index", ViewContext.RouteData.Values["controller"].ToString())
}

@if(ViewContext.RouteData.Values["action"].ToString() != "Index"){
    @Html.ActionLink(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString())
}

Comments

Popular posts from this blog

Linq Exclude from separate list

Sorting Ascending and Descending

Linq Query Syntax vs Method Syntax