Today in this blog we will discuss 3 tag helpers in ASP.Net Core: anchor tag helper, link tag helper, and script tag helper.
Here is the code.
Anchor tag helper
The anchor tag helper renders it as an HTML anchor tag. Anchor tag helper has attributes of asp-controller and asp-action and it also has the asp-route-id attribute to render id-based link or route. If the asp-action attribute value is Index, then no action is appended to the URL, leading to the invocation of the default Index action.
Link tag helper
The link tag helper renders as a link to the primary or fallback css file. The Link Tag Helper allows us to specify a CDN for the CSS file and a fallback when the CDN is not available. The Link Tag Helper provides the performance advantage of a CDN with the robustness of local hosting.
Script tag helper
The script tag helper renders as a link to the primary or fallback css file. The Script Tag Helper allows you to specify a CDN for the script file and a fallback when the CDN is not available. The Script Tag Helper provides the performance advantage of a CDN with the robustness of local hosting.
Step 1. Create an ASP.NET web application project in Visual Studio 2019.
Step 2. Create the class employee under the Models folder.
Step 3. “Add” DbSet in ApplicationDbContext which is located under the data folder of your project.
Step 4. Now in the package manager console Add-migration InitialModel hit enter then update-database. It will add an employee table with all the properties.
Step 5. Open the same HomeController or a new controller and write the following code on the controller class.
Step 6. Now open the index view under the views folder and write the following code.
![Position]()
Step 7. Now “Add” details view click on the details action in the controller class. It will be added in the details view under the views folder as the default name.
![Employee Details]()
Link tag helper example
Now add the following code in _Layout for the Link tag helper.
Link tag helper has some attributes, properties, and methods.
- href: This represents the linked resource. The address is passed through to the created HTML.
- asp-fallback-href: The URL of a CSS stylesheet to fallback to in the case the primary URL fails.
- asp-fallback-test-class: The class name defined in the stylesheet to use for the fallback test.
- asp-fallback-test-property: The CSS property name to use for the fallback test.
- asp-fallback-test-value: The CSS property value to use for the fallback test.
Script tag helper example
- asp-fallback-test: The script method defined in the primary script to use for the fallback test.
- asp-fallback-src: The URL of a Script tag to fallback to in the case the primary one fails.
Complete _Layout code for Link Tag helper and Script tag helper.
Summary
In this article, we have discussed anchor tag helper, link tag helper, and script tag helper. I hope you have understood the use of these 3 tag helpers.