微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TagHelpers在Asp.Net Core 3.1中的ViewComponents内部不起作用

如何解决TagHelpers在Asp.Net Core 3.1中的ViewComponents内部不起作用

我在ViewComponents中使用TagHelpers遇到问题。在ViewComponent剃刀文件中,taghelpers无法正常工作,例如,我有一个如下标签

<a class="btn btn-block btn-outline-secondary"
   asp-action="Index"
   asp-controller="Home">
    @category
</a>

但是诸如 asp-action asp-controller 之类的taghelpers无法正常工作,因此链接无法正确生成。 TagHelpers可以在其他视图上正常工作,但不能在位于以下位置的ViewComponent中工作:

Views/Shared/Components/<Component Name>/Default.cshtml

这是 Components / NavigationMenuViewComponent.cs 中的ViewComponent代码

public class NavigationMenuViewComponent : ViewComponent
{
    private readonly IStoreRepository _repository;

    public NavigationMenuViewComponent(IStoreRepository repository)
    {
        _repository = repository;
    }

    public IViewComponentResult Invoke()
    {
        ViewBag.SelectedCategory = RouteData.Values["category"];
        return View(
            _repository.Products
            .Select(m => m.Category)
            .distinct()
            .OrderBy(m => m)
        );
    }
}

这是_ViewImports.cshtml:

@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelper
@addTagHelper *,Sportsstore.Web

FIX::我发现了问题所在。我在Microsoft.AspNetCore.Mvc上犯了一个错误 TagHelpers

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。