Things about Taghelper-custom taghelper (formatted output, Dependency injection use)

Source: Internet
Author: User
Tags html encode

The final step in customizing Taghelper is to add presentation code to the process method or Processasync method. Familiar with the WebControl development of friends know the Render method, in this method will add the display of HTML elements and launch script, Taghelper This step we have to do is the same as the Render method.

Here we mainly use the second parameter in the above method to output the display part of the view.

First let's look at the definition of the output type taghelperoutput:

    <summary>///Class used to represent the output of an <see cref= "Itaghelper"/&GT;. </summary> public class Taghelperoutput {//<summary>/instantiates a new Instanc        E of <see cref= "Taghelperoutput"/&GT;. </summary>//<param name= "tagName" >the HTML element ' s tag name.</param>//<para M name= "Attributes" >the HTML attributes.</param> public taghelperoutput (string tagName, Idictionary<str        ING, object> attributes) {...}        <summary>//The HTML element ' s tag name. </summary>///<remarks>/A whitespace or <c>null</c> value results in no STA        RT or end tag being rendered.        </remarks> public string TagName {get; set;}        <summary>//The HTML element ' s pre content. </summary>//<remarks>value is prepended to the <see cref= "Itaghelper"/> ' s final output.</remarks> public Taghelperconten        T precontent{get;}        <summary>//The HTML element ' s main content. </summary>//<remarks>value occurs in the <see cref= "Itaghelper"/> ' s final output after &lt See cref= "Precontent"/> and///before <see cref= "postcontent"/></remarks> public Taghelperc        Ontent Content {get;}        <summary>//The HTML element ' s post content. </summary>//<remarks>value is appended to the <see cref= "Itaghelper"/> ' s final output.<        /remarks> public taghelpercontent postcontent {get;} <summary>//<c>true</c> if <see cref= "Content"/> has been set, &LT;C&GT;FALSE&LT;/C&G T        otherwise.        </summary> public bool Iscontentmodified {get;}   <summary>     Indicates whether or not, the tag is self-closing.        </summary> public bool Selfclosing {get; set;}        <summary>//The HTML element ' s attributes. </summary>//<remarks>/MVC would HTML encode <see cref= "string"/> values when Gen Erating the start tag. It would not HTML encode//a <c>Microsoft.AspNet.Mvc.Rendering.HtmlString</c> instance.        MVC converts most other types to A///<see cref= "string"/&GT;, then HTML encodes the result.        </remarks> public idictionary<string, object> Attributes {get;}        <summary>//Changes <see cref= "Taghelperoutput"/> to generate nothing. </summary>//<remarks>//sets <see cref= "TagName"/> to <c>null</c> D clears <see cref= "Precontent"/&GT; <see cref= "Content"/&GT;,///And <see cref= "PostcontEnt "/> to suppress output.    </remarks> public void Suppressoutput () {...} }

TagName:

Specifies the tag to output to the outermost HTML element on the view.

Precontent

Specifies the addition to the HTML element in front of the main content (contents).

Content

Specifies the main content of the HTML element, followed by Precontent, Postcontent front.

Postcontent

Specifies the HTML element behind the main content.

Supressoutput

No display content is generated.

Typically we set these properties in output according to actual needs, with more of the tagname and content, specifying the outermost tag of the generated HTML element in the TagName, and adding its internal HTML elements and startup scripts to the content.

We know that ASP. NET 5 implements dependency injection, and in the Taghelper class we can get more system instance objects through dependency injection, all for the specific needs. We just need to add a property of the relevant type in the Taghelper class, and then add the Activate property on the property header to get the corresponding instance automatically. For example, to get viewcontext information, you can add the following code to the class:

[Activate]public viewcontext viewcontext {get; set;}

This allows us to get the context information for the current view in other places through the property viewcontext.

In this way, you can get more system instance objects, such as ActionContext ,, HttpContext , HttpRequest HttpResponse ,, ViewDataDictionary and ActionBindingContext等 . See here for a specific introduction to dependency injection.

Write at the end

So far, the knowledge of how to customize Taghelper has all been introduced, so let's recall:

1. Define a Taghelper class

2. Design Attributes:properties is Attributes.

3. How to design the embedded Taghelper

4. Format output

Things about Taghelper-custom taghelper (formatted output, Dependency injection use)

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.