【问题标题】:Link not rendering when using asp-page TagHelper使用 asp-page TagHelper 时链接不呈现
【发布时间】:2018-09-28 01:41:14
【问题描述】:

我有一个页面在使用 asp-page TagHelper 时不呈现 HTML 中的链接。我以前见过这个,但这是由于拼写错误或页面不存在。

在下面_Layout中的两个链接中,

  • 用户呈现为http://localhost/ReportGroups/Admin/Users
  • 角色呈现为http://localhost/ReportGroups
  • 手动导航到角色会导致 404 错误。

_Layout.cshtml

该链接是管理菜单的一部分。注释掉的 FontAwesome 图标是我测试的一部分。 /Admin/Users 工作正常,但 /Admin/RoleList 不起作用。以前是/Admin/Roles,但我部分地构建了该文件的新副本,以查看是否有任何错误,或者它是否是保留字。

@if (User.Identity.IsAuthenticated)
{
    if (User.IsInRole("Admin"))
    {
        <div id="nav-admin">
            <ul>
                <li><a asp-page="/Admin/Users"> Users</a></li>@*< i class="fas fa-user-secret" title="Users"></i>*@
                <li><a asp-page="/Admin/RoleList"> Roles</a></li>
                @*<i class="fas fa-id-card" title="Roles"></i>*@
            </ul>
        </div>
    }
}

RoleList.cshtml

@page "{roleId}/{userId}"
@model RoleListModel
@{
    ViewData["PageTitle"] = "Admin Tools - Roles";
    ViewData["IconClass"] = "";
    ViewData["IconTitle"] = "Roles";
}

<table>
    <thead>
        <tr>
            <th>Role</th>
            <th>User Name</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>&nbsp;</th>
        </tr>
    </thead>
    <tbody>
        @foreach (ApplicationRole role in Model.AppRoles)
        {

            <tr>
                <td colspan="4"><strong>@role.Name</strong></td>
                <td>
                    <a asp-page="/App/RoleEdit" asp-route-roleId="@role.Id"><i class="fas fa-edit"></i></a>
                </td>
            </tr>

            IList<ApplicationUser> usersOfRole = Model.AppRoleUsersDict[role];
            foreach (ApplicationUser user in usersOfRole)
            {
                <tr>
                    <td>@role.Name</td>
                    <td>@user.UserName</td>
                    <td>@user.FirstName</td>
                    <td>@user.LastName</td>
                    <td>
                        <form method="post" asp-page-handler="delete">
                            <button type="submit" asp-page="/App/GroupEdit" asp-page-handler="delete" asp-route-roleId="@role.Id" asp-route-userId="@user.Id">
                                <i class="fas fa-trash-alt" style="color:red"></i>
                            </button>
                        </form>
                    </td>
                </tr>
            }
        }

    </tbody>
</table>

RoleList.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ctrack.ReportGroups.Data;
using Ctrack.ReportGroups.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Ctrack.ReportGroups.Pages
{
    public class RoleListModel : PageModel
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly RoleManager<ApplicationRole> _roleManager;
        private readonly ApplicationDbContext _db;

        public RoleListModel(
            ApplicationDbContext db,
            UserManager<ApplicationUser> userManager,
            RoleManager<ApplicationRole> roleManager)
        {
            _db = db;
            _userManager = userManager;
            _roleManager = roleManager;
        }

        public IList<ApplicationRole> AppRoles { get; set; }

        public IDictionary<ApplicationRole, IList<ApplicationUser>> AppRoleUsersDict { get; set; }

        /// <summary>
        /// List all Roles, and the Users assigned to them.  Exposes a form to delete the relationship for each user.
        /// </summary>
        /// <returns>Task&gt;IActionResult&gt;</returns>
        public async Task<IActionResult> OnGetAsync()
        {

            AppRoles = _roleManager.Roles.ToList();
            AppRoleUsersDict = new Dictionary<ApplicationRole, IList<ApplicationUser>>();

            foreach (ApplicationRole role in AppRoles)
            {
                IList<ApplicationUser> usersOfRole = await _userManager.GetUsersInRoleAsync(role.Name);
                AppRoleUsersDict.Add(role, usersOfRole);
            }

            return Page();
        }

        /// <summary>
        /// Removes a role from a User
        /// </summary>
        /// <param name="roleId"><see langword="string"/> containing RoleId</param>
        /// <param name="userId"><see langword="string"/> containing UserId</param>
        /// <returns>Task&gt;IActionResult&gt;</returns>
        public async Task<IActionResult> OnPostDeleteAsync(string roleId, string userId)
        {
            ApplicationUser user = await _userManager.FindByIdAsync(userId);
            ApplicationRole role = await _roleManager.FindByIdAsync(roleId);

            try
            {
                await _userManager.RemoveFromRoleAsync(user, role.Name);
                TempData["Notification"] = $"Successfully removed Role {role.Name} from User {user.UserName}.";
            }
            catch (Exception ex)
            {
                TempData["ErrorMessage"] = $"Exception {ex.GetType()} encountered while removing Role {role.Name} from User {user.UserName}.";
            }
            return Page();
        }
    }
}

_ViewImports.cshtml

@using Microsoft.AspNetCore.Identity
@using CtrackReportGroupsRtca
@using Ctrack.ReportGroups.Data
@using Ctrack.ReportGroups.Identity
@using System.Security.Claims
@using Microsoft.AspNetCore.Html;
@namespace Ctrack.ReportGroups.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

【问题讨论】:

    标签: c# asp.net-core-2.0 razor-pages asp.net-core-tag-helpers


    【解决方案1】:

    我使用的是 .NET Core 2.2,遇到了类似的问题。我通过使用修复它

    asp-page="/ThePage/Index"
    

    而不是

    asp-page="/ThePage"
    

    不过,这可能掩盖了另一个根本问题。

    【讨论】:

    • 这也解决了我的问题。
    • 这也解决了我的问题。根据此处的评论:link 这似乎是设计使然。
    • 这解决了我的问题; Microsoft 文档在他们的示例中令人困惑,它们仅提供目录。
    • 这是设计使然。 asp-page 的参数是页面名称而不是 URL 路径。
    【解决方案2】:

    它不起作用的原因是您没有定义 roleId 和 userId 值。将您的链接更改为如下所示:

    <a asp-page="/Admin/RoleList" asp-route-roleId="YourRoleID" asp-route-userId="YourUserID"> Roles</a>
    

    【讨论】:

    • 奇怪的是我必须定义它(使用无效值)并且它可以工作。与不定义它相反。那好吧。谢谢你的帮助:)
    【解决方案3】:

    就我而言,我忘记在页面顶部包含 @page 指令:

    @page
    
    <h1>Hello, world!</h1>
    

    【讨论】:

      【解决方案4】:

      将此代码包含在您当前的 _ViewImports.cshtml 中,为我解决!

      @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

      【讨论】:

        猜你喜欢
        • 2021-04-14
        • 1970-01-01
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-21
        • 1970-01-01
        相关资源
        最近更新 更多