ASP.NET Core 2.0自定义TagHelper未执行

时间:2018-01-20 16:06:27

标签: asp.net-core asp.net-core-tag-helpers

我有以下TagHelper,它显示用户ID的角色:

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代码永远不会被执行。任何建议都将不胜感激。

我没有看到任何错误或任何暗示问题的提示。代码没有运行。

似乎没有执行代码。

0 个答案:

没有答案