Instalar
https://www.nuget.org/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using AutoMapper; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.Logging; | |
using peliculasAPI.DTOs; | |
using peliculasAPI.Entidades; | |
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 | |
namespace peliculasAPI.Controllers | |
{ | |
[Route("api/generos")] | |
[ApiController] | |
public class GenerosController : ControllerBase | |
{ | |
private readonly ILogger<GenerosController> logger; | |
private readonly ApplicationDbContext context; | |
private readonly IMapper mapper; | |
public GenerosController(ILogger<GenerosController> logger, ApplicationDbContext context, IMapper mapper) | |
{ | |
this.logger = logger; | |
this.context = context; | |
this.mapper = mapper; | |
} | |
[HttpGet] | |
public async Task<ActionResult<List<GeneroDTO>>> Get() | |
{ | |
var generos = await context.Generos.ToListAsync(); | |
return mapper.Map<List<GeneroDTO>>(generos); | |
} | |
} | |
} |
using System; | |
using AutoMapper; | |
using peliculasAPI.DTOs; | |
using peliculasAPI.Entidades; | |
namespace API.Utilidades | |
{ | |
public class AutoMapperProfiles : Profile | |
{ | |
public AutoMapperProfiles() | |
{ | |
CreateMap<Genero, GeneroDTO>(); | |
// CreateMap<Genero, GeneroDTO>().ReverseMap(); | |
} | |
} | |
} |
public class Startup | |
{ | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddAutoMapper(typeof(Startup)); | |
/// ... | |
} | |
} |