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

Getting anchor tag link to open in new tab when using tag helper


Using the new tag helper in asp.net-core-mvc

<a class="custom-social-icon" asp-area="" asp-protocol="https" asp-host="www.youtube.com/mysite" >
    <img src="~/images/youtube.gif" alt="youtube_logo" />
</a>

Is there a setting in the new tag helper that allows the link to open in a new tab? Because you cannot add target="_blank" using this method.

Additionally, when using the tag helper and changing pages, hovering over the icon shows the correct link when using the standard href way (www.youtube.com/mysite) but with the tag helper it adds the page path to the end of the url (www.youtube.com/mysite/Home/Contact). How do you prevent that if using the tag helper?


Solution

  • You could clear the path adding an empty action and controller

    <a asp-action=""
       asp-controller=""
       asp-area=""
       asp-protocol="https"
       asp-host="www.youtube.com/mysite"
       target="_blank">
    

    I got a warning in Visual Studio 2017 from the target attribute

    Attribute is allowed only when 'href' is present

    However the attribute was still rendered to the page

    But all this seems a bit counterproductive, why not just use plain old html

    <a href="https://www.youtube.com/mysite" 
       target="_blank">