我可以将 AutoMapper 8.0.1 与 Blazor 服务器应用程序一起使用吗?我已经尝试过,但我的代码总是遇到错误:
缺少类型映射配置或映射不受支持。映射类型:对象 -> 对象 System.Object -> System.Object
我已将映射器添加到启动文件中:
services.AddAutoMapper(typeof(Startup));
Run Code Online (Sandbox Code Playgroud)
我已经创建了个人资料:
public class MyProfile : Profile
{
public MyProfile()
{
CreateMap<District, DistrictModel>();
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用它:
[Inject]
protected IMapper Mapper { get; set; }
District district = DistrictService.FindDistrictById(districtId);
DistrictModel model = Mapper.Map<DistrictModel>(district);
Run Code Online (Sandbox Code Playgroud)
AssertConfigurationIsValid 方法给出:
Cannot find any profiles with the name 'MyProfile'. (Parameter 'profileName')
Run Code Online (Sandbox Code Playgroud)
Startup.cs
var mapperConfiguration = new MapperConfiguration(configuration =>
{
configuration.AddProfile(new MyProfile());
});
var mapper = mapperConfiguration.CreateMapper();
services.AddSingleton(mapper);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6598 次 |
| 最近记录: |