Should my custom ASP.Net 5 MVC 6 Tag Helpers have an asp- prefix?
Asked Answered
A

2

5

I've been reading about the new support for Tag Helpers in MVC 6 and now need to create my own. I see that the built in tag helpers for existing HTML elements are all prefixed with "asp-"

For example: <a asp-controller="Home" asp-action="Index">Home</a>

But what about my own custom tag helpers. Should I also prefix those with "asp-" to show that it is running on the server. Or should this be reserved for framework attributes? Should I create my own project/company named prefix instead?

Is there any guidance on this subject that I have missed?

Advice answered 11/9, 2015 at 21:59 Comment(1)
Thanks for the edit @Taylor Mullen! hopefully that is enough to get it reopened. I actually really want to know the answer before I go and put my custom tag helpers all through my code only to have to change them later. To be honest I don't think there is any official guidance on this at the moment.Advice
H
5

Tag helpers that target existing HTML elements should preface attribute names with a prefix that indicates the attribute is additive and run on the server. For example, the built in ASP.NET 5 Tag Helpers use the “asp-” prefix. The “asp-” prefix is not considered a reserved prefix, so developers can copy that convention. Some teams will prefer to use their own naming convention to distinguish their Tag Helpers.

If a Tag Helper targets a custom element then attributes should not be prefixed. Custom elements are processed only on the server, so you don’t need the prefix to denote server processing. A good example is the EnvironmentTagHelper. The following markup comes from the Views/Shared/_Layout.cshtml file created by a new ASP.NET web app.

<environment names="Development">

See also Authoring Tag Helpers

Hardback answered 17/9, 2015 at 17:19 Comment(1)
Thanks, really appreciate you clearing this up for me before I get too far with a project.Advice
M
0

Each TagHelper targets one or more specific HTML elements or custom tags.

for example take a look AnchorTagHelper, You can see, TargetElementAttribute is used to associate this TagHelper with the standard HTML a element:

[HtmlTargetElement("a", Attributes = ActionAttributeName)]
//...
public class AnchorTagHelper : TagHelper

So your custom tag helper have its own pre-fixes.

Mishandle answered 16/9, 2015 at 9:28 Comment(2)
The MS policy on this is that it's OK to use "asp-" prefix. See my answer below which is going into our TH documentation.Hardback
@RickAnd-MSFT Thanks for your answer :)Mishandle

© 2022 - 2024 — McMap. All rights reserved.