飞扬的Blog
主页
登录
asp.net core 自定义 TagHelper
"date"
:
"2020-05-25 13:58:25"
"classfiy"
:
"asp.net core"
"author"
:
"飞扬"
"viewTimes"
:
287
返回
官方文档:[点击查看](https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/tag-helpers/authoring?view=aspnetcore-3.1 "点击查看") >发现的问题: 好像不能更改项目的namespace,更改了就无效了,我也不知道什么情况。。 ```c# //TagHelpers\MyTagHelper.cs using Microsoft.AspNetCore.Razor.TagHelpers; namespace FysmartBlog.TagHelpers { [HtmlTargetElement("blog")] public class BlogTagHelper : TagHelper { [HtmlAttributeName("b-action")] public string Action { get; set; } [HtmlAttributeName("b-id")] public string BlogId { get; set; } [HtmlAttributeName("b-title")] public string BlogTitle { get; set; } [HtmlAttributeName("b-fragment")] public string fragment { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { output.TagName = "a"; if (string.IsNullOrEmpty(Action)) { if (!string.IsNullOrEmpty(BlogId)) Action = "details"; else Action = "index"; } var href = $"/{Action.ToLower()}/{BlogId}"; if (Action == "index") href = "/"; if (!string.IsNullOrEmpty(fragment)) href += "#" + fragment; output.Attributes.SetAttribute("href", href); if (!string.IsNullOrEmpty(BlogTitle)) output.Content.SetContent(BlogTitle); } } } ``` ```c# //Views\_ViewImports.cshtml using FysmartBlog @addTagHelper *, FysmartBlog ```