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

好久不见. 提交于 2019-12-21 14:06: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.



来源:https://stackoverflow.com/questions/32532734/should-my-custom-asp-net-5-mvc-6-tag-helpers-have-an-asp-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!