c# - 错误 CS0121:编译时自动映射器出现问题
问题描述
所以映射器在我的项目中不起作用
services.AddAutoMapper();
错误:
Startup.cs(112,21):错误 CS0121:以下方法或属性之间的调用不明确:“ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Assembly[])”和“ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Type[])” [/Users/admin/Desktop/YP/youplay/server/YouPlay.Api/YouPlay.Api.csproj]
当我更改为:
services.AddAutoMapper(typeof(Startup));
它是编译的,但是,当我试图使 req 到
public async Task<ActionResult<BetOpenForm>> CreateBetAsync([FromBody]CreateBetForm bet)
{
BetOpenForm response;
try
{
BetOpen newBet = await _betService.CreateBetOpenAsync(bet);
response = _mapper.Map<BetOpenForm>(newBet);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
// return new BadRequestObjectResult(new ApiError("Something gone wrong ;("));
return new BadRequestObjectResult(new ApiError(ex.Message));
}
return Ok(response);
}
我得到:
楷模:
using System;
using System.Collections.Generic;
namespace YouPlay.Api.Controllers.ViewModels.Bet
{
public class BetOpenForm
{
public string Id { get; set; }
public int StreamId { get; set; }
public List<BetOptionForm> BetOptions { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string AuthorId { get; set; }
}
}
using System;
namespace YouPlay.Api.Controllers.ViewModels.Bet
{
public class BetOptionForm
{
public string Id { get; set; }
public string BetOpenId { get; set; }
public string OptionText { get; set; }
public double Odd { get; set; }
public decimal Amount { get; set; }
public int BetsCount { get; set; }
}
}
解决方案
推荐阅读
- sas - 如何打开以变量值命名的文件,该变量的值在 SAS 中跨行更改
- javascript - 无法将 HTML 结构包裹在元素周围
- css - 如何将 svg 路径变形动画合并到 React 应用程序中
- angular - 为什么运行 Angular 项目时会出现“TypeError: argument fn must be a function”错误?
- android - 有没有 ibm watson 的 android sdk 文档
- swift - SwiftUI - 在不修改视图大小的情况下使用 GeometryReader
- reactjs - page.data.json 404 未找到 & componentDidMount() 未在生产中触发
- command - 如何找到我正在使用的命令的版本?
- java - 如何在 selenium 中创建等待 web 元素完成刷新的等待
- python - 如何遍历 n 个列表中的所有 n 元组