Пользовательский TagHelper ASP.NET Core 2.0 не выполняется

У меня есть следующий TagHelper, который отображает роли для идентификатора пользователя:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Razor.TagHelpers;

using UserManagement.Models;

namespace UserManagement.TagHelpers
{
    [HtmlTargetElement("td", Attributes = "user-roles")]
    public class UserRolesTagHelper : TagHelper
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly RoleManager<IdentityRole> _roleManager;

        public UserRolesTagHelper(
            UserManager<ApplicationUser> userMgr, 
            RoleManager<IdentityRole> roleMgr
        )
        {
            _userManager = userMgr;
            _roleManager = roleMgr;
        }

        [HtmlAttributeName("user-roles")]
        public string User { get; set; }

        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            List<string> names = new List<string>();

            ApplicationUser user = await _userManager.FindByIdAsync(User);

            if (user != null)
            {
                foreach (var role in _roleManager.Roles)
                {
                    if (role != null && await _userManager.IsInRoleAsync(user, role.Name))
                    {
                        names.Add(role.Name);
                    }
                }
            }

            output.Content.SetContent(names.Count == 0 ? "No Roles" : string.Join(", ", names));
        }
    }
}

У меня такая точка зрения:

@model IEnumerable<ApplicationUser>

@{
    ViewData["Title"] = "User Accounts";
}

<div class="bg-default panel-body">
    <h4>@ViewData["Title"]</h4>
</div>

<div class="text-danger" asp-validation-summary="ModelOnly"></div>

<table class="table table-condensed table-bordered table-striped">
    <tr>
        @*<th>UserName</th>*@
        <th>Name</th>
        <th>Email</th>
        <th>Roles</th>
        <th></th>
    </tr>

    @if (Model.Count() == 0)
    {
        <tr>
            <td colspan="3" class="text-center">
                No User Accounts
            </td>
        </tr>
    }
    else
    {
        foreach (ApplicationUser user in Model)
        {
            <tr>
                @*<td>@user.UserName</td>*@
                <td>@user.Name</td>
                <td>@user.Email</td>
                <td user-roles="@user.Id"></td>
                <td>
                    <a asp-controller="UserAdmin" asp-action="Edit" asp-route-id="@user.Id">
                        <i class="fas fa-edit"></i>
                    </a>
                    <a asp-controller="UserAdmin" asp-action="Delete" asp-route-id="@user.Id">
                        <i class="fas fa-window-close"></i>
                    </a>
                </td>
            </tr>
        }
    }
</table>

<a class="btn btn-default" asp-action="Create">Create</a>
<a class="btn btn-default" asp-controller="Home" asp-action="Index">Home</a>

Затем в _ViewImports.cshtml я добавил следующее:

 @addTagHelper *, UserManagement

По какой-то причине код TagHelper никогда не запускается. Любые предложения будут ценны.

Я не вижу никаких ошибок или намеков на проблему. Код просто не работает.

Кажется, он никогда не выполняет код.


person codecypher    schedule 20.01.2018    source источник
comment
См. Мой ответ здесь .   -  person Kirk Larkin    schedule 21.01.2018
comment
Это была опечатка. Я это сделал.   -  person codecypher    schedule 24.01.2018