简体   繁体   English

如何在 ASP.NET Core 2.2 API 中使用 Automapper

[英]How to use Automapper with ASP.NET Core 2.2 API

This is the error I get:这是我得到的错误:

System.InvalidOperationException: Unable to resolve service for type 'myBackEnd.Entities.Striper' while attempting to activate 'myBackEnd.Controllers.StripeController'. System.InvalidOperationException:尝试激活“myBackEnd.Controllers.StripeController”时无法解析“myBackEnd.Entities.Striper”类型的服务。 at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)

I am working on using Automapper for the first time.我正在第一次使用 Automapper。 I am also new to NET.我也是 NET 的新手。 I am using an HTTPPOST to get data from the front end in the format我正在使用 HTTPPOST 从前端以格式获取数据

    amount :  string;
    currency :  string;
    description :  string;
    token :  string;
    name: string;
    address_city: string;
    address_line1: string;
    address_line2: string;
    address_state: string;
    address_zip: string;
    address_country: string;

I have stripe.cs and stripeDto.cs files:我有 stripe.cs 和 stripeDto.cs 文件:

public class Striper
{
    public object Amount { get; set; }
    public string Currency { get; set; }
    public object Description { get; set; }
    public string Token { get; set; }
    public string Name { get; set; }
    public string Address_line1 { get; set; }
    public string Address_line2 { get; set; }
    public string Address_state { get; set; }
    public string Address_zip { get; set; }
    public string Address_country { get; set; }
}

stripeDto:条纹:

public class StripeDto
{
    public object Amount { get; set; }
    public string Currency { get; set; }
    public object Description { get; set; }
    public string Token { get; set; }
    public string Name { get; set; }
    public string Address_line1 { get; set; }
    public string Address_line2 { get; set; }
    public string Address_state { get; set; }
    public string Address_zip { get; set; }
    public string Address_country { get; set; }
}

This is the mapping profile file:这是映射配置文件:

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Striper, StripeDto>();
        CreateMap<StripeDto, Striper>();
    }
}

Finally this is the Controller:最后这是控制器:

private readonly AppDbContext _context;
private IMapper _mapper;

public StripeController(AppDbContext context, IMapper mapper)
{
     _context = context;
     _mapper = mapper;
}

public async Task<IActionResult> PostCreditCardData([FromBody] StripeDto stripeDto)
{
        Console.WriteLine("got this from the front end", stripeDto);
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

          _context.StripeDto.Add(stripeDto);
         // Instantiate source object stripe
         await _context.SaveChangesAsync();

        _striper = _mapper.Map<Striper>(stripeDto);
        return Ok(_striper);


 }

I get this error in visual studio "Unable to resolve service for type 'myBackEnd.Entities.Striper'"我在 Visual Studio 中收到此错误“无法解析‘myBackEnd.Entities.Striper’类型的服务”

Here is the startup.cs code:这是startup.cs代码:

services.AddAutoMapper();

First, you must install Automapper dependency injection package:首先,您必须安装 Automapper 依赖注入包:

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection安装包 AutoMapper.Extensions.Microsoft.DependencyInjection

Call services.AddAutoMapper() in ConfigureServices method in the Startup class.在 Startup 类的 ConfigureServices 方法中调用 services.AddAutoMapper()。

More on this at: https://dotnetcoretutorials.com/2017/09/23/using-automapper-asp-net-core/有关更多信息,请访问: https : //dotnetcoretutorials.com/2017/09/23/using-automapper-asp-net-core/

Your AutoMapper configuration in Startup class should be as follows:您在Startup类中的AutoMapper配置应如下所示:

public void ConfigureServices(IServiceCollection services)
{

    // Auto Mapper Configurations
    var mappingConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new MappingProfile());
    });

    IMapper mapper = mappingConfig.CreateMapper();
    services.AddSingleton(mapper);

    //........
}

I know it's too late我知道为时已晚

but it's because of your startup and AutoMapper configuration但这是因为您的启动和 AutoMapper 配置

services.AddAutoMapper(typeof(MappingProfile).Assembly); services.AddAutoMapper(typeof(MappingProfile).Assembly);

best regards此致

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在启用 ASP.NET Core 2.2 EndpointRouting 的情况下使用 RouteDataRequestCultureProvider? - How to use RouteDataRequestCultureProvider with ASP.NET Core 2.2 EndpointRouting enabled? 在 ASP.NET 核心 Web ZDB974238714CA8DE634A7CE1D0FZA8 的 class 库项目中使用 AutoMapper - Use AutoMapper inside a class library project in ASP.NET Core Web API Web Api in.Net Core中如何使用AutoMapper? - How to use AutoMapper in Web Api in .Net Core? 在 asp.net 核心 2.2 上上传文件 API 时不能使用替换 function - can't use replace function when upload files API on asp.net core 2.2 ASP.NET Core 2.2:无法解析“AutoMapper.IMapper”类型的服务 - ASP.NET Core 2.2: Unable to resolve service for type 'AutoMapper.IMapper' Serilog无法使用asp.net core 2.2 API中的配置 - Serilog not working from configuration in asp.net core 2.2 API 如何在Automapper和Asp.Net Core依赖注入中使用通用配置文件 - How to use generic Profile with Automapper and Asp.Net Core Dependency Injection 如何使用 ASP.NET Core Web API 2.2 进行分页和过滤器 - How to make pagination and filters with ASP.NET Core Web API 2.2 如何在ASP.Net Core 2.2 Web Api应用程序中连接到Amazon Kinesis Firehose - How to connect to Amazon Kinesis Firehose in ASP.Net Core 2.2 Web Api Application 如何检查API请求中的值声明(ASP.NET Core 2.2) - How to check claim for value in API request (ASP.NET Core 2.2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM