在Asp.Net Core中,我重写了IdentityUser表以添加更多功能
public class Person : IdentityUser
{
public string Location {get; set; }
public SellerType SellerType{get; set; }
}
public enum SellerType
{
PrivateSeller ,
Dealer
}
我还有一个ViewModel类CredentialModel,我使用AutoMapper链接Person类
public class CredentialModel
{
public string UserName { get; set; }
public string Password { get; set; }
public string FirstName {get;set;}
public string LastName {get; set; }
public string Location {get; set; }
public string SellerType{get; set; }
public string[] SellerTypes {get; set; }
public string PhoneNumber {get; set; }
}
AutoMapper Profile Class
public class UserMapProfile : Profile
{
public UserMapProfile(){
CreateMap<CarAdderUser, CredentialModel>()
.ForMember(model=> model.SellerType ,
opt=> opt.MapFrom(vm=>((SellerType)vm.SellerType).ToString()))
.ForMember(model=> model.SellerTypes, opt=> opt.UseValue(Enum.GetNames(typeof(SellerType)).ToArray()))
.ReverseMap();
}
}
我的问题在于控制器
像这样它可以工作,但我返回实体而不是VM
[HttpGet("getUser/{username}")]
public IActionResult GetUser(string username)
{
var user= _userManager.FindByNameAsync(username);
return Ok(user);
}
就像这样,它会在邮递员中抛出404;
[HttpGet("getUser/{username}")]
public IActionResult GetUser(string username)
{
var user= _userManager.FindByNameAsync(username);
return Ok(_mapper.Map<CredentialModel>(user));
}
IT给我一个错误
An unhandled exception has occurred: Missing type map configuration or unsupported mapping.
Mapping types:
Task`1 -> CredentialModel
不知道该怎么办......不是AutoMapper专家
答案 0 :(得分:0)
解决了...问题是我忘记了我只需要结果...... 所以user.Result而不是用户在返回... 不确定其他内容是什么......它们不是DB中User表中的列
答案 1 :(得分:0)
你不应该使用user.Result
。这将阻止调用线程。请改用async/await
。
public async Task<IActionResult> GetUser(string username)
{
var user= await _userManager.FindByNameAsync(username);
return Ok(_mapper.Map<CredentialModel>(user));
}