Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anchor tag helper to open view in new window-tab

Using an anchor tag helper, how can we open an ASP.NET Core MVC View in a new browser window-tab. I tried the following but first it complained that target attribute need to have href attribute as well. But, as we know, we can't use the href attribute with asp-action attribute in MVC Core; otherwise we get the error shown below. NOTE: I've seen some suggestions like this one, but they are not tag helper related:

<a asp-action="testAction" href="#" target="_blank">Click Here</a>

Error:

InvalidOperationException: Cannot override the 'href' attribute for . An with a specified 'href' must not have attributes starting with 'asp-route-' or an 'asp-action', 'asp-controller', 'asp-area', 'asp-route', 'asp-protocol', 'asp-host', or 'asp-fragment' attribute.

like image 806
nam Avatar asked Jan 23 '17 04:01

nam


People also ask

What is the difference between HTML helper and tag helpers?

Unlike HtmlHelpers, a tag helper is a class that attaches itself to an HTML-compliant element in a View or Razor Page. The tag helper can, through its properties, add additional attributes to the element that a developer can use to customize the tag's behavior.

What can the ASP page tag helper do?

The Anchor Tag Helper enhances the standard HTML anchor ( <a ... > </a> ) tag by adding new attributes. By convention, the attribute names are prefixed with asp- . The rendered anchor element's href attribute value is determined by the values of the asp- attributes.

What happens when you apply the tag helper opt out character?

The opt-out character (“!”) is used to disable the Tag Helper at the element level. With the opt-out character, the HTML will not be generated for the label tag in the above case. We can use this opt-out character if we want to conditionally control rendering of the HTML elements.


1 Answers

I’m not sure if you’re asking a question or if you’re sharing your findings?

As @Mohamed Rozza mentioned in the comments, if you disregard the Visual Studio warning about the target attribute only allowed when the href is present, then you’ll quickly realize that the hyperlink actually works and opens in a new tab. Regardless of Visual Studio complaining.

As you’ve also pointed out, there is an alternative/workaround where you could create your link like this:

<a href="@Url.Action("testAction","Home")" target="_blank">Click Here</a>

But, as you’ve said, this approach is not tag helper related. But so what?

My questions are these:

  • How important is it for you and your project to be 100% tag helper related?
  • Is it a must?
  • Is it a show-stopper if you don’t always use tag helpers?
  • Can you live with workarounds?
  • Can you live with Visual Studio showing you a warning?

You have two working example that achieves your task.

  1. One by disregarding the VS warning
  2. The other by using a workaround using the Url.Action()

None of these two approaches are bad/wrong. If for some reason you feel compelled to write your own custom tag helper to overcome this...then by all means, go ahead!

If you wish to report a bug to Microsoft about the tag helper not supporting the target attribute without the href, then by all means go ahead!

And finally, if I do not understand the need to be 100% tag helper related (or if I’m over simplifying stuff) then by all means, feel free to share as we may offer a better alternative.

like image 88
Vlince Avatar answered Sep 19 '22 23:09

Vlince