Skip to content

Instantly share code, notes, and snippets.

@ChrisWay
Last active August 29, 2018 06:34
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ChrisWay/3508812 to your computer and use it in GitHub Desktop.
Save ChrisWay/3508812 to your computer and use it in GitHub Desktop.
ASP.NET MVC - Twitter Bootstrap navbar link helper
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());
}
}
Copy link

ghost commented Aug 29, 2018

Thanks! But it doesn't work for ASP.NET Core anymore.
I've just updated it: https://gist.github.com/biehlermi/934509124eb726cefc4b833996c53420

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment