如何修复Microsoft.AspNetCore.Mvc.RazorPages.PageActionDescriptor'以键入'Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor

时间:2019-10-08 07:07:49

标签: asp.net-core asp.net-core-identity

  

InvalidCastException:无法将类型为“ Microsoft.AspNetCore.Mvc.RazorPages.PageActionDescriptor”的对象转换为类型为“ Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor”。   RoleManagerController.cs中的App.Web.Controllers.RoleManagerController.GetDynamicPermissionActions(),第273行

 [HttpGet("{roleName}/permissions", Name = "GetRolePermissions")]
    public async Task<IActionResult> RolePermissions(string roleName)
    {
        var role = await _roleManager.Roles
            .Include(x => x.Claims)
            .SingleOrDefaultAsync(x => x.Name == roleName);

        if (role == null)
        {
            return NotFound();
        }

        var dynamicActions = this.GetDynamicPermissionActions();

        return View(new RolePermissionViewModel
        {
            Actions = dynamicActions,
            Role = role
        });
    }
 private List<ActionDtoViewModel> GetDynamicPermissionActions()
    {
        var actions = new List<ActionDtoViewModel>();

        var actionDescriptors = _actionDescriptor.ActionDescriptors.Items;

        foreach (var actionDescriptor in actionDescriptors)
        {
            var descriptor = (ControllerActionDescriptor)actionDescriptor;

            var hasPermission = descriptor.ControllerTypeInfo.GetCustomAttribute<AuthorizeAttribute>()?
                                    .Policy == ConstantPolicies.DynamicPermission ||
                                descriptor.MethodInfo.GetCustomAttribute<AuthorizeAttribute>()?
                                    .Policy == ConstantPolicies.DynamicPermission;

            if (hasPermission)
            {
                actions.Add(new ActionDtoViewModel
                {
                    ActionName = descriptor.ActionName,
                    ControllerName = descriptor.ControllerName,
                    ActionDisplayName = descriptor.MethodInfo.GetCustomAttribute<DisplayAttribute>()?.Name,
                    AreaName = descriptor.MethodInfo.GetCustomAttribute<AreaAttribute>()?.RouteValue
                });
            }
        }

        return actions;
    }

0 个答案:

没有答案