C# 在ViewComponent中调用IdentityUser? 使用Microsoft.AspNetCore.Identity; 使用Microsoft.AspNetCore.Mvc; 使用服务和网站模型; 使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Security.Claims; 使用System.Threading.Tasks; 命名空间服务\u网站.ViewComponents { 公共类CurrentUser:ViewComponent { 私有只读用户管理器_UserManager; 专用只读签名管理器\u签名管理器; public CurrentUser(UserManager UserManager、SignInManager SignInManager) { _userManager=userManager; _signInManager=signInManager; } 公共应用程序用户调用() { if(_signInManager.IsSignedIn((ClaimsPrincipal)用户)) { var userId=\u userManager.GetUserId((ClaimsPrincipal)User); var UserFinded=\u userManager.FindByIdAsync(userId).Result; 返回userfind; } 返回null; } } }
这是我的代码,我想在Invoke方法中返回ApplicationUser或IdentityUser的对象,但是当我这样编写代码时,我得到了这个错误。 我该怎么做C# 在ViewComponent中调用IdentityUser? 使用Microsoft.AspNetCore.Identity; 使用Microsoft.AspNetCore.Mvc; 使用服务和网站模型; 使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Security.Claims; 使用System.Threading.Tasks; 命名空间服务\u网站.ViewComponents { 公共类CurrentUser:ViewComponent { 私有只读用户管理器_UserManager; 专用只读签名管理器\u签名管理器; public CurrentUser(UserManager UserManager、SignInManager SignInManager) { _userManager=userManager; _signInManager=signInManager; } 公共应用程序用户调用() { if(_signInManager.IsSignedIn((ClaimsPrincipal)用户)) { var userId=\u userManager.GetUserId((ClaimsPrincipal)User); var UserFinded=\u userManager.FindByIdAsync(userId).Result; 返回userfind; } 返回null; } } },c#,asp.net-core,asp.net-core-mvc,asp.net-identity,asp.net-core-viewcomponent,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Identity,Asp.net Core Viewcomponent,这是我的代码,我想在Invoke方法中返回ApplicationUser或IdentityUser的对象,但是当我这样编写代码时,我得到了这个错误。 我该怎么做 错误 InvalidOperationException:视图组件仅支持返回字符串、IHtmlContent或IViewComponentResult 将ApplicationUser包装在ViewModel中 视图模型 using Microsoft.AspNetCore.Identity; using Microsoft.AspN
错误 InvalidOperationException:视图组件仅支持返回字符串、IHtmlContent或IViewComponentResult
将ApplicationUser包装在ViewModel中 视图模型
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Services_Website.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Services_Website.ViewComponents
{
public class CurrentUser : ViewComponent
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
public CurrentUser(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
public ApplicationUser Invoke()
{
if (_signInManager.IsSignedIn((ClaimsPrincipal)User))
{
var userId = _userManager.GetUserId((ClaimsPrincipal)User);
var UserFinded = _userManager.FindByIdAsync(userId).Result;
return UserFinded;
}
return null;
}
}
}
查看组件代码
public class CurrentUserVM
{
public ApplicationUser ApplicationUser {get; set;}
}
视图组件视图
public IViewComponentResult Invoke()
{
ApplicationUser user = null;
if (_signInManager.IsSignedIn((ClaimsPrincipal)User))
{
var userId = _userManager.GetUserId((ClaimsPrincipal)User);
user = _userManager.FindByIdAsync(userId).Result;
}
return new CurrentUserVM()
{
ApplicationUser = user;
}
}
@model CurrentUserVM
// your view code