A simple set of extension methods to help you figure out whether you are on a particular route or not. Also a nice helper method to generate routes based on a name of a route. You still need to provide route values that are not set in the defaults.
Available via NuGet
PM> Install-Package WhatRoute
1. Install the package using NuGet.
2. Add the WhatRoute.Core namespace to your views web.config
3. Start accessing the extension methods
You have the following methods availble:
Url.PathTo(string routeName, object routeValues = null)
Url.IsActive(string action, string controller, string area = "")
Url.IsActive(object routeValues = null, object querystringValues = null)
Url.IsActiveRoute(string routeName, object routeValues = null, object querystringVaules = null)
Html.IsActiveCss(object routeValues = null, object querystringValues = null, string cssClass = “active”)
Html.IsActiveCss(string action, string controller, string area = "", string cssClass = “active”)
Html.IsActiveRouteCss(string routeName, object routeValues = null, object querystringValues = null)
<ul class="example">
<li class="@Html.IsActiveRouteCss("root")">
<a href="@Url.PathTo("root")">Root</a>
</li>
<li class="@Html.IsActiveCss("index", "home")">
<a href="@Url.Action("index", "home", new { area = "" })">Default Page Home#Index</a>
</li>
<li class="@Html.IsActiveCss(new { action = "show", controller = "home", id = 1 })">
<a href="@Url.Action("show", "home", new { id = 1, area = "" })">Show Page with id Show#Index id of 1</a>
</li>
<li class="@Html.IsActiveCss(new { action = "show", controller = "home", id = 2 })">
<a href="@Url.Action("show", "home", new { id = 2, area = "" })">Show Page with id Show#Index id of 2</a>
</li>
<li class="@Html.IsActiveCss(new { action = "another", controller = "home" }, new { q = "guess" })">
<a href="@Url.Action("another", "home", new { q = "guess", area = "" })">Show Page with querystring of "guess"</a>
</li>
<li class="@Html.IsActiveCss(new { action = "index", controller = "testarea", area = "other" })">
<a href="@Url.Action("index", "testarea", new { area = "other" })">Index in an area TestArea#Index</a>
</li>
<li class="@Html.IsActiveCss(new { action = "gross", controller = "home", a = 1, really = 2, gross = 3, path = 4 })">
<a href="@Url.Action("gross", "home", new { area = "", a = 1, really = 2, gross = 3, path = 4 })">A really gross path</a>
@if (Url.IsActive(new {action = "gross", controller = "home", a = 1, really = 2, gross = 3, path = 4})) { @Html.Partial("Sample"); }
</li>
</ul>
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don’t break it in a
future version unintentionally. - Send me a pull request. Bonus points for topic branches.
Written by Khalid Abuhakmeh
- 1.1.0 : added PathTo and IsActiveRoute / IsActiveRouteCss
- 1.0.0 : initial release