The things about Taghelper.

Source: Internet
Author: User

Profile

Taghelper is a new feature of ASP. NET 5. Perhaps when you have not heard of it, it has caused a lot of discussion among technicians, and even part of it is called the regression of server controls. In fact, it's just a simplified version that mixes HTML and server content without the control life cycle, State hold, and events. It does not have access to all of the page's content, like server controls. It can only access content generated by itself.

What is Taghelper?

We used to use Htmlhelps in the MVC project. For example, when you need to add a navigation bar to the view, we add the following code to the page:

<ol>...<li> @Html. ActionLink ("Home", "Index", "Home") <li>...</ol>

Here are some HTML elements. There are some C # code at the beginning of the @, and these C # code will be parsed into HTML elements when the view is parsed.

When we use Taghelper, we can also use the following code to achieve the same effect:

<ol>...<li><a controller= "Home" action= "Index" >Home</a></li>...</ol>

Here, the properties of the A element controller and action are not HTML5 properties, but the Taghelper properties.

One thing to emphasize here is that although Taghelper looks a bit like the way we were before the server control, it is not a return to the server control. It is more like giving the user a concise way to express the user's intentions. There is no such thing as life cycle in itself. It will preprocess the server content and then assign the value to the corresponding property. Let's take a look at a usage of Selecttaghelper provided by Microsoft:

<select asp-for= "Country" asp-items= "viewbag.countries" >

Where the text "country" is assigned to the Asp-for property, the variable viewbag.countries is assigned to the attribute Asp-items.

How to use Taghelpers?

on how to use Taghelpers, we can refer to a few steps:

  1. Add dependencies for the package with Taghelpers in the Project.json file;
  2. Register Taghelper on the view used:
     @addTagHelper "[the full type name of Taghelper, The assembly name "

    First parameter is the full name of the Taghelper class, and when you only need to use one of the Taghelper, you can specify the full name of the Taghelper class you are using here, including its namespace. If you want to use all the taghelpers in the assembly, you can use "*" here or omit this parameter. For example, if you need to use Microsoft-provided taghelpers can be registered by adding the following code:

     @addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers "

    or

     @addTagHelper" Microsoft.AspNet.Mvc.TagHelpers "

    If you only want to use Anchortaghelper, then just the code:

     @addTagHelper "Microsoft.AspNet.Mvc.TagHelpers.AnchorTagHelper, Microsoft.AspNet.Mvc.TagHelpers "

    The second parameter is the assembly name.
    Of course, if you want to cancel the registration of a taghelper, you can use Removetaghelper, such as

     @removeTagHelper "Microsoft.AspNet.Mvc.TagHelpers.AnchorTagHelper, Microsoft.AspNet.Mvc.TagHelpers "

    Once the taghelper is unregistered, its corresponding tag cannot be parsed into this taghelper.

  3. Add Taghelper where you need to use the view, for example we add a anchortaghelper:
    <a asp-controller= "Home" asp-action= "about" >About</a>
Written at the end of this article

The above introduces some basic knowledge of taghelper, this is knowledge is based on Asp.net5 beta4, ASP. NET5 is still in development, has now released the BETA5, some things have been changing, so it may be written on some of the method names, the assembly name in the new version has changed, this need to be noted.

Next I will introduce some of the taghelpers that Microsoft has offered us and how to develop our own taghelper.

The things about Taghelper.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.