protected void Application_Start()
{
//Create Mapping
Mapper.Initialize(cfg =>
{
cfg.CreateMap();
});
}
[HttpGet]
public IHttpActionResult Get()
{
var source = new List
{
new SourceObject { Id = 1, Name = "Object1"},
new SourceObject { Id = 2, Name = "Object2"}
};
var destination = Mapper.Map, List>(source);
return Ok(destination);
}
在上面的示例中,我们首先创建了一个源对象列表,然后使用AutoMapper将其映射到目标对象列表,并返回目标对象列表作为响应。