在.NET Web API上使用AutoMapper(非核心)
我们下面有这个web api,我们希望使用AutoMapper简化到DTO的代码映射,反之亦然,但我们还没有找到好的设置以及如何使用它,因为大多数文档都使用.Net Core或旧版本的AutoMapper AutoMapper的新版本是版本9 从我所看到的大多数情况来看,映射非常直接,但在我们的例子中,我们有一个CategoryName,它映射到x.Category.Name等 有人能在这里指一下正确的方向吗 谢谢在.NET Web API上使用AutoMapper(非核心),.net,asp.net-web-api2,.net,Asp.net Web Api2,我们下面有这个web api,我们希望使用AutoMapper简化到DTO的代码映射,反之亦然,但我们还没有找到好的设置以及如何使用它,因为大多数文档都使用.Net Core或旧版本的AutoMapper AutoMapper的新版本是版本9 从我所看到的大多数情况来看,映射非常直接,但在我们的例子中,我们有一个CategoryName,它映射到x.Category.Name等 有人能在这里指一下正确的方向吗 谢谢 [HttpGet] [Route("api/v1/Activities/{sor
[HttpGet]
[Route("api/v1/Activities/{sortfields=Id}/{pagenumber=1}/{pagesize=10}")]
[CacheOutput(ClientTimeSpan = 60, ServerTimeSpan = 60)]
public async Task<IHttpActionResult> GetActivities(string sortfields, int pageNumber, int pageSize)
{
string userId = User.Identity.GetUserId();
if (!IsOkPropertyValidate(sortfields))
{
return BadRequest("Sort property is incorrect");
}
var activities = await db.Activities
.Include(b => b.User)
.Include(c => c.Category)
.Where(q => q.UserId == userId).ToListAsync();
var noOfRecords = activities.Count();
var activitiesDTO = await (db.Activities
.Include(b => b.User)
.Include(c => c.Category)
.Where(q => q.UserId == userId)
.Select(x => new ActivityDTO
{
Id = x.Id,
OwnerName = x.User.FirstName + " " + x.User.LastName,
CategoryName = x.Category.Name,
Name = x.Name,
Description = x.Description,
NoOfMinutes = x.NoOfMinutes,
DateCreated = x.DateCreated,
DateModified = x.DateModified,
})
.AsQueryable()
.ApplySort(sortfields)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)).ToListAsync();
var data = new
{
Metadata = new {
TotalRecords = noOfRecords,
CurrentPageSize = pageSize,
CurrentPage = pageNumber,
TotalPages = (int)Math.Ceiling(noOfRecords / (double)pageSize)
},
Results = activitiesDTO
};
return Ok(paginationMetadata);
}
[HttpGet]
[路由(“api/v1/Activities/{sortfields=Id}/{pagenumber=1}/{pagesize=10}”)]
[CacheOutput(ClientTimeSpan=60,ServerTimeSpan=60)]
公共异步任务GetActivities(字符串sortfields、int pageNumber、int pageSize)
{
字符串userId=User.Identity.GetUserId();
如果(!IsOkPropertyValidate(sortfields))
{
返回BadRequest(“排序属性不正确”);
}
var活动=等待数据库活动
.Include(b=>b.User)
.包括(c=>c.类别)
.Where(q=>q.UserId==UserId).toListSync();
var noOfRecords=activities.Count();
var activitiesDTO=await(db.Activities
.Include(b=>b.User)
.包括(c=>c.类别)
.Where(q=>q.UserId==UserId)
.选择(x=>new ActivityDTO
{
Id=x.Id,
OwnerName=x.User.FirstName+“”+x.User.LastName,
CategoryName=x.Category.Name,
Name=x.Name,
描述=x.描述,
NoOfMinutes=x.NoOfMinutes,
DateCreated=x.DateCreated,
DateModified=x.DateModified,
})
.AsQueryable()
.ApplySort(sortfields)
.Skip((页码-1)*页面大小)
.Take(pageSize)).ToListAsync();
var数据=新
{
元数据=新{
TotalRecords=noOfRecords,
CurrentPageSize=页面大小,
CurrentPage=页码,
TotalPages=(int)数学上限(noOfRecords/(double)pageSize)
},
结果=活动到
};
返回Ok(分页元数据);
}
您需要在ModelMappingProfile类中映射属性
public class ModelMappingProfile : Profile
{
public ModelMappingProfile()
{
CreateMap<Category, ActivityDTO>()
.ForMember(dto => dto.CategoryName , opts =>
opts.MapFrom(src => src.Category.Name));
}
}
公共类ModelMappingProfile:Profile
{
公共ModelMappingProfile()
{
CreateMap()
.ForMember(dto=>dto.CategoryName,opts=>
opts.MapFrom(src=>src.Category.Name));
}
}
检查这是否有帮助:查看和。我改为这样做:私有MapperConfiguration配置=新的MapperConfiguration(cfg=>cfg.CreateMap().FormMember(dto=>dto.OwnerName,conf=>conf.MapFrom(ol=>ol.User.FirstName+“”+ol.User.LastName)).FormMember(dto=>dto.CategoryName,conf=>conf.MapFrom(ol=>ol.Category.Name));