我正在使用AutoMapper.Extensions.microsoft.DependencyInjection 7.0.0
我只需要为具有特定角色的用户映射属性,否则在asp.net core 3.0项目中使用自动映射器忽略映射。
这是我尝试过的,
public class BranchProfile : Profile
{
private readonly HttpContext _httpContext;
public BranchProfile(IHttpContextaccessor httpContextaccessor)
{
_httpContext = httpContextaccessor.HttpContext;
CreateMap<Branch,BranchEditViewModel>()
.ForMember(dest => dest.Lock,opt => opt.Condition(src => _httpContext.User.IsInRole(UserRoles.Master.ToString())))
.ForMember(dest => dest.Expireon,opt => opt.Condition(src => _httpContext.User.IsInRole(UserRoles.Master.ToString())));
}
}
但是我得到以下无参数的构造函数异常。
没有为'BranchProfile'类型定义无参数构造函数
有没有更好的方法访问HttpContext
来进行此条件映射?请协助