Should my custom ASP.Net 5 MVC 6 Tag Helpers have

2019-04-08 18:12发布

问题:

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?

回答1:

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



回答2:

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.