HMTLHelperExtensions.cs 761 B

123456789101112131415161718192021
  1. using Microsoft.AspNetCore.Mvc.Rendering;
  2. namespace NewWebApplicationSample
  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. controller = actualController;
  12. if (string.IsNullOrEmpty(action))
  13. action = actualAction;
  14. return (controller == actualController && action == actualAction) ? activeClass : string.Empty;
  15. }
  16. }
  17. }