Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom anchor tag helper with hash string support

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.

like image 755
Vahid Amiri Avatar asked Jul 27 '16 18:07

Vahid Amiri


People also ask

How do I create a tag helper?

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.

What is the difference between Htmlhelper and TagHelper?

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.

Which of the following is correct syntax to create a tag using HTML helper?

@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.

Which is the correct syntax of tag helper in form tag?

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.


1 Answers

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>
like image 62
tmg Avatar answered Oct 12 '22 01:10

tmg