gpt4 book ai didi

c# - 手动实例化和调用 TagHelpers

转载 作者:行者123 更新时间:2023-12-05 06:28:10 26 4
gpt4 key购买 nike

这是对 this question 的后续问题,这似乎适用于较旧的 ASP.NET Core 版本(我使用的是 2.1)。

我正在尝试从 TagHelper 中手动调用 TagHelper。应用上面链接问题中的答案,TagHelper.Process 看起来像这样:

public override async void Process(TagHelperContext context, TagHelperOutput output)
{
var anchorTagHelper = new AnchorTagHelper
{
Action = "Home",
};
var anchorOutput = new TagHelperOutput("a", new TagHelperAttributeList(), (useCachedResult, encoder) => new HtmlString());
var anchorContext = new TagHelperContext(
new TagHelperAttributeList(new[] { new TagHelperAttribute("asp-action", new HtmlString("Home")) }),
new Dictionary<object, object>(),
Guid.NewGuid());
await anchorTagHelper.ProcessAsync(anchorContext, anchorOutput);
output.Content.SetHtmlContent(anchorOutput);
}

此时出现了几个编译器错误。

cannot convert from 'System.Guid' to 'string'

没问题,我可以转换为字符串。

There is no argument given that corresponds to the required formal parameter 'value' of 'HtmlString.HtmlString(string)

检查 TagHelperOutput constructor 的 MSDN 页面, 它似乎不再需要 HtmlString 了。

我将该参数更改为委托(delegate)函数:

new TagHelperOutput("a", new TagHelperAttributeList(),
(useCachedResult, encoder) => Task.Factory.StartNew<TagHelperContent>(
() => new DefaultTagHelperContent()));

最后的编译错误:

There is no argument given that corresponds to the required formal parameter 'generator' of 'AnchorTagHelper.AnchorTagHelper(IHtmlGenerator)

希望它是一个可选参数,我传入了 null:

var anchorTagHelper = new AnchorTagHelper(null);

它终于编译了——但这导致了运行时的空指针异常:

Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.Process(TagHelperContext context, TagHelperOutput output)

因此我的问题是:如何实例化 AnchorTagHelper这样我就可以在 C# 中手动调用 TagHelpers 了吗?

编辑:我已经对 IHtmlGenerator 进行了 DI,并得到了一条更有意义的错误消息:

private IHtmlGenerator htmlGenerator;
public myAnchorTagHelper(IHtmlGenerator htmlGenerator) {
this.htmlGenerator = htmlGenerator;
}

Value cannot be null. Parameter name: viewContext>

Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateActionLink(ViewContext viewContext, String linkText, String actionName, String controllerName, String protocol, String hostname, String fragment, Object routeValues, Object htmlAttributes) at Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Process(TagHelperContext context, TagHelperOutput output) at EAGLEweb2020.Models.EAGLEinputTagHelper.Process(TagHelperContext context, TagHelperOutput output) in C:\Users\1135937\source\repos\EAGLEweb2020\EAGLEweb2020\Models\TagHelpers\EAGLEinputTagHelper.cs:line 66 at Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output) at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.d__0.MoveNext()

但是我怎样才能 DI 一个 ViewContext?

编辑 2:

原来 ViewContext 是 AnchorTagHelper 中的公共(public)属性:

AnchorTagHelper inputTagHelper = new AnchorTagHelper(htmlGenerator);
inputTagHelper.ViewContext = viewContext;

但是现在内容是空的...

最佳答案

你好,我正在经历这个,你被卡在了 ViewContext 和输出上。我做过类似的项目。这是代码,希望对您有所帮助。

[HtmlTargetElement(ParentAnchorTag)]
public class ParentActionTagHelper : TagHelper
{
private const string ParentAnchorTag = "p-a";

[HtmlAttributeNotBound]
[ViewContext]
public ViewContext viewContext { get; set; }

private readonly IHtmlGenerator _htmlGenerator;

public ParentActionTagHelper(IHtmlGenerator htmlGenerator)
{
_htmlGenerator = htmlGenerator;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";

var anchorTagHelper = new AnchorTagHelper(_htmlGenerator)
{
Action = "Privacy",
ViewContext = viewContext,

};
var anchorOutput = new TagHelperOutput("a", new TagHelperAttributeList(),
(useCachedResult, encoder) => Task.Factory.StartNew<TagHelperContent>(
() => new DefaultTagHelperContent()));
anchorOutput.Content.AppendHtml("Privacy Link");
var anchorContext = new TagHelperContext(
new TagHelperAttributeList(new[]
{
new TagHelperAttribute("asp-action", new HtmlString("Privacy"))
}),
new Dictionary<object, object>(),
Guid.NewGuid().ToString());

anchorTagHelper.ProcessAsync(anchorContext, anchorOutput).GetAwaiter().GetResult();
output.Content.SetHtmlContent(anchorOutput);
}
}

关于c# - 手动实例化和调用 TagHelpers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54582450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com