I'm trying to create a custom ASP.NET Core tag helper for a
tag that supports asp-hash
attribute. What that attribute is supposed to do is just append the provided value to the end of the href
attribute.
<a asp-controller="Home" asp-action="Index" asp-hash="mainDiv">some link</a>
would then generate:
<a href="http://localhost/home/index#mainDiv">some link</a>
I found the source code for AnchorTagHelper
in this section in asp.net github repo but I can't find a way to append stuff to the end of generated href
.
To create custom tag helper, the first step is to create a class that inherits from "TagHelper" class. This class has a virtual method to generate HTML tags. It contains both synchronous (Process) and asynchronous (ProcessAsync) implementation of the virtual method.
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.
@addTagHelper makes Tag Helpers available The code above uses the wildcard syntax ("*") to specify that all Tag Helpers in the specified assembly (Microsoft. AspNetCore. Mvc. TagHelpers) will be available to every view file in the Views directory or subdirectory.
The syntax to register all the tag helpers that are in an assembly is to use asterisk comma (*,) and then the assembly name, Microsoft. AspNet. Mvc. TagHelpers.
No asp-hash
attribute exists, but there is no need to make a custom anchor tag helper. You are looking for asp-fragment
attribute:
<a asp-controller="Home" asp-action="Index" asp-fragment="mainDiv">some link</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With