TagHelper(标签助手)是ASP.NET Core非常好的一种新特性。可以扩展视图,让其看起来像一个原生HTML标签。

应该使用TagHelper替换HtmlHelper,因其更简洁更易用,且支持依赖注入。可以通过其构造函数中注入所需要的服务。

一、扩展的标签:

下面使用一个简单的标签示例扩展:

 [HtmlTargetElement("hello")]
public class HelloTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "p";
output.Attributes.Add("id", context.UniqueId);
output.Attributes.Add("style", "color:red;font-weight:bold;");
output.PreContent.SetContent("Hello ");
output.PostContent.SetContent($", time is now: {DateTime.Now.ToString("HH:mm:")}");
}
}

在此定义了一个“hello”标签,可以像其他标识一样使用。

不过前提得先将该标签所在的命名空间引用到到cshtml文件中(此处使用"_ViewImports.cshtml"进行设置)

 @addTagHelper "*, TagAbout"

在View中使用,如在Index.cshtml中使用,如下:

<div class="col-md-3">
<hello>Jackie Lee</hello>
</div>

运行效果:

产生的HTML如下:

二、扩展的标签属性:

定义a、p、ul、li、button、span、div标签的属性扩展TagHelper类如下,为其内容最后添加一段通过依赖注入进来的类调用返回的内容。

并为其添加title属性,以提示“author-for"所设置的内容:

  [HtmlTargetElement("a", Attributes = ForAttributeName)]
[HtmlTargetElement("p", Attributes = ForAttributeName)]
[HtmlTargetElement("ul", Attributes = ForAttributeName)]
[HtmlTargetElement("li", Attributes = ForAttributeName)]
[HtmlTargetElement("button", Attributes = ForAttributeName)]
[HtmlTargetElement("span", Attributes = ForAttributeName)]
[HtmlTargetElement("div", Attributes = ForAttributeName)]
public class AuthorTagHelper : TagHelper
{
private const string ForAttributeName = "author-for";
private const string TextAttributeName = "content"; [HtmlAttributeName(ForAttributeName)]
public string AuthorFor { get; set; } private ContentManager _contentManager; public AuthorTagHelper(ContentManager contentManager)
{
_contentManager = contentManager;
} public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.Attributes.Add("title", AuthorFor);
output.PostContent.AppendHtml($"<span style='font-weight:bold;color:orange;'>[{_contentManager.GetContent()}]</span>");
// 可用于验证
if (false)
{
var builder = output.Content;
output.SuppressOutput(); // nothing to output
builder.AppendHtml(string.Empty);
}
}
}

依赖注入的类:

public class ContentManager
{
public static ContentManager ContentMgr { get; private set; } = new ContentManager();
public string GetContent()
{
return $"Well, this is the injected data by the tag helper.";
}
}

在Startup的ConfigureServices中添加依赖注入对象:

services.AddSingleton(ContentManager.ContentMgr);

在View中使用如下:

<div class="col-md-3">
<hello>Jackie Lee</hello>
<a href="#" author-for="Hello, I'm Jackie Lee.">Welcome to China!</a>
<div author-for="Jackie Lee">Now it is the very good tag.</div>
<p author-for="Well done.">Nice to meet you.</p>
<span author-for="Nice, this is what does for you only.">*</span>
</div>

运行效果:

产生的HTML内容:

asp.net core的TagHelper简单使用的更多相关文章

  1. ASP.NET Core MVC TagHelper实践HighchartsNET快速图表控件-开源

    ASP.NET Core MVC TagHelper最佳实践HighchartsNET快速图表控件支持ASP.NET Core. 曾经在WebForms上写过 HighchartsNET快速图表控件- ...

  2. Hangfire在ASP.NET CORE中的简单实现

    hangfire是执行后台任务的利器,具体请看官网介绍:https://www.hangfire.io/ 新建一个asp.net core mvc 项目 引入nuget包 Hangfire.AspNe ...

  3. 给 asp.net core 写一个简单的健康检查

    给 asp.net core 写一个简单的健康检查 Intro 健康检查可以帮助我们知道应用的当前状态是不是处于良好状态,现在无论是 docker 还是 k8s 还是现在大多数的服务注册发现大多都提供 ...

  4. ASP.NET Core 基础教程总结 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 基础教程总结 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 基础教程总结 ASP.NET Core 基础教程总算是有了个简单 ...

  5. ASP.NET Core 登录登出 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 登录登出 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 登录登出 上一章节我们总算完善了注册的功能,而且也添加了一个用户,现 ...

  6. ASP.NET Core 新增用户 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 新增用户 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 新增用户 上一章节我们实现了一个注册表单,但也留了一些东西还没完成, ...

  7. ASP.NET Core 用户注册 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 用户注册 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 用户注册 上一章节我们终于迁移完了 Identity 的数据,也创建 ...

  8. ASP.NET Core Identity 迁移数据 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Identity 迁移数据 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 迁移数据 上一章节中我们配置了 ...

  9. ASP.NET Core Identity 配置 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Identity 配置 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 配置 上一章节我们简单介绍了下 Id ...

随机推荐

  1. EasyPusher应用

    转自https://github.com/EasyDarwin/EasyPusher 本文仅实际体验一下demo,分析一下如何应用. 1)EasyPusher框图预览 2) EasyPusher应用实 ...

  2. net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法

    使用Hibernate manytoone属性关联主表的时候,如果使用JSONArray把pojo对象转换成json对象时,很容易出现循环的异常.解决的办法就是, 在转换json对象时忽略manyto ...

  3. vtkQuadratic创建半球面

    用的关键类:vtkQuadric.vtkSampleFunction.vtkContourFilter:用于创建方框的类vtkOutlineFilter #ifndef INITIAL_OPENGL ...

  4. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  5. HTML5的File API读取文件信息

    html结构: <div id="fileImage"></div> <input type="file" value=" ...

  6. poi2015 bzoj4377-4386训练

    就按时间顺序写吧 完成度:10/10 3.30 bzoj4385 首先一定是删去连续d个数,然后枚举终点,起点显然有单调性,用单调队列乱搞搞就可以啦 bzoj4378 首先才结论:可行当且仅当把所有大 ...

  7. 常用js代码集

    <img src="{:url('publics/verify')}" onclick="this.src='{:url('publics/verify')}'&q ...

  8. Nagios安装

    在做安装之前确认要对该机器拥有root权限. 确认你安装好的Fedora系统上已经安装如下软件包再继续: Apache GCC编译器 GD库与开发库 可以用yum命令来安装这些软件包: yum ins ...

  9. 【JS】字符串操作

    1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符 ...

  10. jquery.validate不用submit而用js提交的例子

    $("#form").validate(); $("#btn).click(function(){ if($("#form").valid()){ $ ...