Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.AspNetCore.Mvc.Rendering;
  8. using Microsoft.AspNetCore.Mvc.Routing;
  9. using Microsoft.AspNetCore.Mvc.TagHelpers;
  10. using Microsoft.AspNetCore.Mvc.ViewFeatures;
  11. using Microsoft.AspNetCore.Razor.TagHelpers;
  12.  
  13. namespace mirrando.TagHelpers
  14. {
  15. /// <summary>
  16. /// <see cref="ITagHelper"/> implementation targeting <menulink> elements that assist with rendering contextually aware menu links.
  17. /// If the current route is matched the given <menulink> will be active. This was added to demonstrate how a TagHelper might be used
  18. /// with Semantic UI to implement a simple menu.
  19. /// </summary>
  20. [HtmlTargetElement("menulink", Attributes = "controller-name, action-name, menu-text")]
  21. public class MenuLinkTagHelper : TagHelper
  22. {
  23. public string ControllerName { get; set; }
  24. public string ActionName { get; set; }
  25. public string MenuText { get; set; }
  26.  
  27. [ViewContext]
  28. public ViewContext ViewContext { get; set; }
  29.  
  30. public override void Process(TagHelperContext context, TagHelperOutput output)
  31. {
  32. var urlHelper = new UrlHelper(ViewContext);
  33.  
  34. string menuUrl = urlHelper.Action(ActionName, ControllerName);
  35.  
  36. output.TagName = "a";
  37. output.Attributes.Add("href", $"{menuUrl}");
  38. output.Attributes.Add("class", "item blue");
  39. output.Content.SetContent(MenuText);
  40.  
  41. var routeData = ViewContext.RouteData.Values;
  42. var currentController = routeData["controller"];
  43. var currentAction = routeData["action"];
  44.  
  45. if (String.Equals(ActionName, currentAction as string, StringComparison.OrdinalIgnoreCase)
  46. && String.Equals(ControllerName, currentController as string, StringComparison.OrdinalIgnoreCase))
  47. {
  48. output.Attributes.SetAttribute("class", "active item blue");
  49. }
  50.  
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement