Razor 如何在donet core 2.1中创建自定义taghelper

Razor 如何在donet core 2.1中创建自定义taghelper,razor,asp.net-core-2.1,Razor,Asp.net Core 2.1,我正在dot net core2.1上创建一个MVC应用程序。我试图为razor创建一个html和ajax taghelper。我创建了image actionlink。我以前在.net MVC 5上使用扩展方法做过这件事,但在.net core中我无法理解。任何人都可以帮助我实现这一点,因为我只是一个初学者。首先,我建议您看看如何创建标记帮助程序。如果你仍然感到困惑,别担心,我会给你一个更简单的解释 TagHelper不过是一段服务器端代码,用于帮助在Razor中呈现HTML元素。使TagHe

我正在dot net core2.1上创建一个MVC应用程序。我试图为razor创建一个html和ajax taghelper。我创建了image actionlink。我以前在.net MVC 5上使用扩展方法做过这件事,但在.net core中我无法理解。任何人都可以帮助我实现这一点,因为我只是一个初学者。

首先,我建议您看看如何创建
标记帮助程序。如果你仍然感到困惑,别担心,我会给你一个更简单的解释

TagHelper
不过是一段服务器端代码,用于帮助在Razor中呈现HTML元素。使
TagHelper
更强大的是我们可以将配置和服务注入其中

让我们以图像为例。假设我们想要一个魔术
,它将被渲染为

<img src="localserver-url" />
<img src="cdn-url" />

谢谢你的建议和指示。我开始查看官方文件,并很快用一个例子进行更新。
[HtmlTargetElement("magic-image")]
public class MagicImageTagHelper : TagHelper
{
    public IConfiguration Configuration{ get; set; } 
    public MagicImageTagHelper(IConfiguration configuration) {
        this.Configuration = configuration;
    }

    // the `Src` here will be set by user in this way : `<magic-image src="xxxx">`
    public string Src { get; set; }

    // how to map the `<magic-image src="xxx">` to the final output
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "img";    // the element will be <img>

        // ...
        // get cdn base url string from configration , 
        var CdnUrlBase = Configuration["CdnUrl"];

        var src=CdnUrlBase + Src; 

        output.Attributes.SetAttribute("src", src);   // set the `img.src`
    }
}