Quantcast
Viewing latest article 7
Browse Latest Browse All 12

Adding Prefixes to Tag Helpers in MVC 6

Some people have said that they would prefer all Tag Helper elements in MVC 6 to be prefixed. I honestly don’t see myself doing this but it is easy to turn on if you prefer tag helper elements to be prefixed with some special text.

Simply add the @tagHelperPrefix directive to the _ViewImports.cshtml file in your project:

@tagHelperPrefix "th:"
@tagHelperPrefix "th:"

Now, Razor will only recognize elements as Tag Helpers if the elements are prefixed with “th:”.

Image may be NSFW.
Clik here to view.
image

You can choose whatever prefix you want for your project. As I said, I probably won’t be using this myself but at least there is an easy way to turn on tag helper prefixes for those who want to be very explicit about tag helpers.

One nice thing with prefixes is that it enables is a quick way to identify what tag helpers exist in a project. When you type in the prefix, IntelliSense will show you a list of elements that can be processed by tag helpers:

Image may be NSFW.
Clik here to view.
image

What do you think? Prefix or not prefix?


Viewing latest article 7
Browse Latest Browse All 12

Trending Articles