自定义TagHelper的最后一步就是在Process方法或ProcessAsync方法中添加展现代码。熟悉WebControl开发的朋友都知道Render方法,在这个方法中会添加展现的Html元素和启动脚本,TagHelper的这一步我们要做的也就是和Render方法一样。

  这里我们主要利用上面方法中的第二个参数output来往View上输出展现部分。

  首先让我们看以output类型TagHelperOutput的定义:

    /// <summary>
/// Class used to represent the output of an <see cref="ITagHelper"/>.
/// </summary>
public class TagHelperOutput
{
/// <summary>
/// Instantiates a new instance of <see cref="TagHelperOutput"/>.
/// </summary>
/// <param name="tagName">The HTML element's tag name.</param>
/// <param name="attributes">The HTML attributes.</param>
public TagHelperOutput(string tagName, IDictionary<string, object> attributes)
{
...
} /// <summary>
/// The HTML element's tag name.
/// </summary>
/// <remarks>
/// A whitespace or <c>null</c> value results in no start 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 TagHelperContent PreContent{ get; } /// <summary>
/// The HTML element's main content.
/// </summary>
/// <remarks>Value occurs in the <see cref="ITagHelper"/>'s final output after <see cref="PreContent"/> and
/// before <see cref="PostContent"/></remarks>
public TagHelperContent 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, <c>false</c> 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 will HTML encode <see cref="string"/> values when generating the start tag. It will not HTML encode
/// a <c>Microsoft.AspNet.Mvc.Rendering.HtmlString</c> instance. MVC converts most other types to a
/// <see cref="string"/>, 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>, and clears <see cref="PreContent"/>, <see cref="Content"/>,
/// and <see cref="PostContent"/> to suppress output.
/// </remarks>
public void SuppressOutput()
{
...
}
}

  TagName:

  指定输出到View上最外层Html元素的Tag。

  PreContent

  指定添加到Html元素主内容(Content)前面的。

  Content

  指定Html元素的主内容,在PreContent后面,PostContent前面。

  PostContent

  指定Html元素主内容后面的。

  SupressOutput

  不生成任何展示内容。

通常我们会根据实际需要设置output中这些属性,其中用的比较多的就是TagName和Content,在TagName指定生成HTML元素的最外层Tag,在Content添加其内部的Html元素和启动脚本。

我们知道ASP.NET 5实现了依赖注入,在TagHelper类中我们也可以通过依赖注入获取更多的系统实例对象,为具体需求所有。我们只需要在TagHelper类中,添加一个相关类型的属性,然后在属性头上添加Activate属性即可自动获取对应实例。比如,获取ViewContext信息,可以在类中添加如下代码:

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

这样我们就可以在其他地方,通过属性ViewContext获取当前View的上下文信息。

通过这种方式,你可以获取到更多的系统实例对象,如ActionContextHttpContextHttpRequestHttpResponse、 ViewDataDictionary以及ActionBindingContext等。具体关于依赖注入的介绍见这里

写在结尾

到此为止,关于如何自定义TagHelper的知识点已经全部介绍完毕,我们来回忆一下:

1. 定义一个TagHelper类

2. 设计Attributes: Properties are Attributes.

3. 如何设计内嵌的TagHelper

4. Format输出  

最新文章

  1. Lesson 12 Goodby and good luck
  2. 《JavaScript高级程序设计》学习笔记(3)——变量、作用域和内存问题
  3. ListView 和 Adapter用法
  4. 集合类(Objective-C &amp; Swift)
  5. Oracle性能优化--DBMS_PROFILER
  6. Visual C++ unicode and utf8 转换
  7. struts2学习笔记(5)---自己定义拦截器
  8. GlusterFS简单配置
  9. Sicily 1133. SPAM
  10. 关于Xcode7的HTTP请求不到网络的问题
  11. String、StringBuffer和StringBuilder
  12. 屏幕旋转时调用PopupWindow update方法更新位置失效的问题及解决方案
  13. 数据库优化案例——————某知名零售企业ERP系统
  14. laravel5增删改查
  15. 【游记】THUWC2018踹线记
  16. MySQL、Mariadb 复制原理
  17. Android短信管家视频播放器代码备份
  18. oAuth2.0认证流程图
  19. ES6装饰器Decorator基本用法
  20. 错误:未启用当前数据库的SQL Server Service Broker,因此查询通知不受支持。如果希望使用通知,请为此数据库启用 Service Broker。

热门文章

  1. objective-c Quick Reference
  2. 尝试 TFS Express 2012.3
  3. 开源IDS系列--snorby 2.6.2 undefined method `run_daily_report&#39; for Event:Class (NoMethodError)
  4. python模拟QQ聊天室(tcp加多线程)
  5. LeetCode 260. Single Number III(只出现一次的数字 III)
  6. LeetCode 118. 杨辉三角
  7. c# 递归异步获取本地驱动器下所有文件
  8. JAVA编程思想读书笔记(一)--面向对象
  9. scrapy抓取拉勾网职位信息(六)——反爬应对(随机UA,随机代理)
  10. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory