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. .htaccess语法之RewriteCond与RewriteRule指令格式详细解释

    htaccess语法之RewriteCond与RewriteRule指令格式详细解释 (2012-11-09 18:09:08) 转载▼ 标签:  htaccess it 分类: 网络 上文htacc ...

  2. Openxml 笔记

    用openxml 生成Excel: private void GenerateExcelUsingOpenxml(DataTable dataTable, string GeneratePath)   ...

  3. UVA 1401 Remember the Word

    字典树优化DP                                Remember the Word Time Limit: 3000MS   Memory Limit: Unknown ...

  4. D3.js学习(三)

    上一节中,我们已经画出了图表,并且给图表添加了坐标轴的标签和标题,在这一节中,我们将要学习几个绘制线条不同特性的几个函数,以及给图表添加格栅.ok,进入话题! 如何给线条设置绘制的样式? 这个其实非常 ...

  5. IOS中div contenteditable=true无法输入

    在IOS中<div contenteditable="true"></div>中点击时可以弹出键盘但是无法输入.加一个样式-webkit-user-sele ...

  6. 让ecshop模板支持php运算

    让ecshop模板支持php运算在 cls_template.php 底部加入函数: /** * 处理if标签 * * @access public * @param string $tag_args ...

  7. HDU4411 最小费用流

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4411 floyd处理出最短路 每个点拆为i.i+n,i到i+n连一条容量为1,费用为负无穷的边,代表这个城 ...

  8. web自动化工具-Browsersync

    web自动化工具-Browsersync browser-sync才是神器中的神器,和livereload一样支持监听所有文件.可是和livereload简单粗暴的F5刷新相比,browsersync ...

  9. 生产环境中,数据库升级维护的最佳解决方案flyway

    官网:https://flywaydb.org/ 转载:http://casheen.iteye.com/blog/1749916 1.  引言 想到要管理数据库的版本,是在实际产品中遇到问题后想到的 ...

  10. poi2015 bzoj4377-4386训练

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