Professional Documents
Culture Documents
3 PDF
3 PDF
NET Core
Assistant Lecturer
❖ They look just like standard HTML code but it is processed by Razor engine
on the server giving it all the advantageous of server-side rendering.
❖ They manipulate the HTML elements on which they are operated or insert
new HTML element, or replace the existing content with the new one.
❖ For Example, we can rewrite the <form> tag as shown below. Here the
asp-action & asp-controller are attributes of the Form Tag Helper.
Render
Purpose of Tag Helpers
❖ Tag Helpers simplify the code required to generate the view’s HTML output
based on the data provided to it. For Example Label Tag Helper generates
the caption based on the display Data Annotation attribute.
❖ Remember that Tag Helpers does not replace the HTML helpers, we can use
both of them side by side as per our requirement.
How to use Tag Helpers?
❖ The ASP.NET Core MVC Tag Helpers resides in the assembly
Microsoft.AspNetCore.Mvc.TagHelpers assembly. We need to import it to
use the Tag Helpers.
➢ 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 the view.
How to use Tag Helpers?
❖ Making Tag Helpers available globally
➢ You can add the @addTagHelper directive to the _ViewImports.cshtml, which makes
it to available in all the views.
➢ Now, The x: must be prefixed to every tag helper in the view, so as to enable on them.
Example
What Tag Helpers provide?
❖ An HTML-friendly development experience
➢ The Tag Helpers look like standard HTML elements. The Front-end Developers need not
learn the C# or razor syntax to add these elements in the view. and thus more easily
achieving the separation which concerns.
❖ Extensible
➢ ASP.NET Core provides many built-in Tag Helpers to help us to create the view. But if
these Helpers do not suit your needs, then you can create your own Tag Helper.
Built-in Tag Helpers
Tag Helper Target Attributes
Form Tag Helper <Form> asp-action, asp-all-route-data, asp-area, asp-controller, asp-fragment, asp-
host, asp-page, asp-page-handler,asp-protocol,asp-route, asp-route-
Cache Tag Helper <cache> enabled, expires-after, expires-on, expires-sliding, priority, vary-by