Search code examples
c#asp.net-coreasp.net-core-tag-helpersasp.net-core-viewcomponent

how to resolve ~ root link from a TagHelper


In ASP.NET 5 MVC6 RC1 - I have a ViewComponent which is designed to represent my traditional "left hand side of the screen" main menu.

I am writing my first TagHelper to represent each menu item link.

I am stuck at the part where I am trying to create a hyperlink.

How do I resolve ~/dashboard/summary?

If I display the menu on this page, the link appears as /dashboard/~/dashboard/summary.

@Url.Content("...") displays @Url.Content("...") i.e. is not processed as razor. The tag helper outputs pure.

Ideally, I would like the solution to be .NET Core compatible as I am eventually aiming for a .net core deployable solution.

See below:

namespace Website
{

    /// <summary>
    /// <MainMenuLink area="" controller="" action=""></MainMenuLink>
    /// 
    /// to render
    /// 
    ///  <a href="~/account/manage/ChangePassword" class="list-group-item @GetClassName("manage", "changepassword")">
    ///    <p class="list-group-item-text"><i class="fa fa-terminal"></i>&nbsp;&nbsp;Change my password</p>
    /// </a>
    /// 
    /// 
    /// </summary>
    [HtmlTargetElement(Attributes = "area, controller, action")]
    public class MainMenuLinkTagHelper : TagHelper
    {
        [HtmlAttributeName("area")]
        public string Area { get; set; }

        [HtmlAttributeName("controller")]
        public string Controller { get; set; }

        [HtmlAttributeName("action")]
        public string Action { get; set; }

        public UrlHelper urlHelper { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "a";    // Works    
            // Stuck here - I want ~/ to resolve to the base root.  
            // At the moment the address is here is localhost:XXXXX/dashboard/~/dashboard/summary

            // Would prefer to use a method which can be used with .net core and not System.Web

            output.Attributes.Add("href", "~/dashboard/summary");
            output.Content.SetHtmlContent("Click me");

        }        
        /// <summary>            
    }
}

Thanks! Dan.


Solution

  • Add a constructor dependency for IUrlHelper in your tag helper. Then use the same extension methods you can use in a view to generate the urls like IUrlHelper.Action(actionName, controllerName):

    private IUrlHelper urlHelper;
    ...
    public MainMenuLinkTagHelper (IUrlHelper urlHelper)
    {
        this.urlHelper = urlHelper;
    }
    ...
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "a";
        output.Attributes.Add("href", this.urlHelper.Action(this.Action, this.Controller));
        output.Content.SetHtmlContent("Click me");
    } 
    

    Edit: ASP Core version 1.0

    It is no longer possible to inject directly an IUrlHelper. You need to inject both IActionContextAccessor and IUrlHelperFactory, then get an IUrlHelper with them. For example:

    private IUrlHelper UrlHelper => 
        this.urlHelperFactory.GetUrlHelper(this.actionAccessor.ActionContext);