HMTLHelperExtensions.cs 820 B

12345678910111213141516171819202122232425
  1. using Microsoft.AspNetCore.Mvc.Rendering;
  2. namespace WebApplicationSample.Helpers
  3. {
  4. public static class HtmlHelpers
  5. {
  6. public static string IsActive(this IHtmlHelper html, string controller = null, string action = null, string activeClass = "active")
  7. {
  8. var actualAction = (string)html.ViewContext.RouteData.Values["action"];
  9. var actualController = (string)html.ViewContext.RouteData.Values["controller"];
  10. if (string.IsNullOrEmpty(controller))
  11. {
  12. controller = actualController;
  13. }
  14. if (string.IsNullOrEmpty(action))
  15. {
  16. action = actualAction;
  17. }
  18. return controller == actualController && action == actualAction ? activeClass : string.Empty;
  19. }
  20. }
  21. }