如何在 ASP.NET Core 中手动注册 AutoMapper 配置文件?

gro*_*kky 2 c# automapper asp.net-core

我正在使用带有内置容器的 ASP.NET Core。

自动注册完成,像这样

var config = new MapperConfiguration(c => c.AddProfiles(typeof(Startup)));
services.AddSingleton<IMapper>(s => config.CreateMapper());
Run Code Online (Sandbox Code Playgroud)

这会自动 1) 配置 AutoMapper,以及 2) 注册在程序集中找到的所有配置文件。

但我想手动注册我的个人资料。我怎么做?

Tse*_*eng 5

只需使用该方法的sigular版本。

var config = new MapperConfiguration(c => {
    c.AddProfile<ProfileA>();
    c.AddProfile<ProfileB>();
});
services.AddSingleton<IMapper>(s => config.CreateMapper());
Run Code Online (Sandbox Code Playgroud)