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. mysql 关系表 分组读取的方法

    关系表 是一个一对多的表 我们用的时候往往希望得到 array( a=>array(1,2,3,4....), b=>array(3,4,5,6,7...) ) 这样的数组 所以我们可以使 ...

  2. div 纯数字很长时无法折行解决

    <div style="width:100%;word-wrap:break-word;word-spacing:normal;"> </div> 加上红色 ...

  3. Web安全之SQL注入攻击技巧与防范

    http://www.plhwin.com/2014/06/13/web-security-sql/

  4. Docker 清理命令集锦

    杀死所有正在运行的容器 复制代码代码如下: docker kill $(docker ps -a -q) 删除所有已经停止的容器 复制代码代码如下: docker rm $(docker ps -a ...

  5. 为什么margin-top不是作用于父元素【margin外边距合并问题】

    coding时发现margin-top居然没作用于本元素上,而是作用到了父元素上. 原来是margin外边距合并导致的.以下是网上搬运来的知识: margin外边距合并详解:外边距合并现象在网页布局中 ...

  6. rcu机制

    转载自:再谈Linux内核中的RCU机制-MagicBoy2010-ChinaUnix博客 http://blog.chinaunix.net/uid-23769728-id-3080134.html ...

  7. 现代软件工程作业-- GitHub的学习

    1.注册github账号: 2.在github上面新建一个名为HelloWord的项目: 3.将本组的其他成员纳入到HelloWorld中: 4.复制远端仓库的地址: 5.在本地的git bash中使 ...

  8. MySQL的简单使用

    MySQL 参数     参数 描述 备注 -D,--database=.name 打开指定数据库 mysql –uroot –procky –Dhisdb 或者mysql –uroot –prock ...

  9. 基于thinkphp的数组分页

    function array_page($array,$rows){ import("ORG.Util.Page"); //导入分页类 $count=count($array); ...

  10. Java中Runnable和Thread的区别

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...