Last active
August 29, 2018 06:34
-
-
Save ChrisWay/3508812 to your computer and use it in GitHub Desktop.
ASP.NET MVC - Twitter Bootstrap navbar link helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc.Html; | |
public static class HtmlHelpers | |
{ | |
public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName) | |
{ | |
var currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action"); | |
var currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller"); | |
var builder = new TagBuilder("li") | |
{ | |
InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToHtmlString() | |
}; | |
if (controllerName == currentController && actionName == currentAction) | |
builder.AddCssClass("active"); | |
return new MvcHtmlString(builder.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! But it doesn't work for ASP.NET Core anymore.
I've just updated it: https://gist.github.com/biehlermi/934509124eb726cefc4b833996c53420