Я новичок в .NET 6 и пытаюсь настроить базовый API, который будет хранить коллекционные карточки в базе данных в качестве практики.
Однако при попытке протестировать запрос POST Я получаю сообщение об ошибке автосопоставления.
An unhandled exception has occurred while executing the request.
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Это моя программа.cs. Ошибка находится в следующей строке:
var cardModel = mapper.Map(cardCreateDto);
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using AutoMapper;
using Api.Data;
using Api.Dtos;
using Api.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Connect to server using connection string from appSettings.Development.json and user-secrets
var sqlConBuilder = new SqlConnectionStringBuilder();
sqlConBuilder.ConnectionString = builder.Configuration.GetConnectionString("SQLDbConnection");
sqlConBuilder.UserID = builder.Configuration["UserId"];
sqlConBuilder.Password = builder.Configuration["Password"];
builder.Services.AddDbContext(opt => opt.UseSqlServer(sqlConBuilder.ConnectionString));
builder.Services.AddScoped();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("api/v1/cards", async (ICardRepo repo, IMapper mapper) => {
var cards = await repo.GetAllCards();
return Results.Ok(mapper.Map(cards));
});
app.MapGet("api/v1/cards/{id}", async (ICardRepo repo, IMapper mapper, int id) => {
var card = await repo.GetCardById(id);
if (card != null)
return Results.Ok(mapper.Map(card));
return Results.NotFound();
});
app.MapPost("api/v1/cards", async (ICardRepo repo, IMapper mapper, CardCreateDto cardCreateDto) => {
var cardModel = mapper.Map(cardCreateDto);
await repo.CreateCard(cardModel);
await repo.SaveChanges();
var cardReadDto = mapper.Map(cardModel);
return Results.Created($"api/v1/cards/{cardReadDto.Id}", cardReadDto);
});
app.Run();
Подробнее здесь: https://stackoverflow.com/questions/781 ... st-request