我是。net的新手,所以我决定使用。net Core,而不是学习“老方法”。我在这里找到了一篇关于为。net Core设置AutoMapper的详细文章,但是对于新手来说还有更简单的操作指南吗?
当前回答
关于这个问题的答案, 不需要在控制器构造函数中指定IMapper映射器参数。
您可以使用Mapper,因为它是代码任何位置的静态成员。
public class UserController : Controller {
public someMethod()
{
Mapper.Map<User, UserDto>(user);
}
}
其他回答
theutz的回答很好,我只想补充一点:
如果你让你的映射配置文件继承自MapperConfigurationExpression而不是profile,你可以非常简单地添加一个测试来验证你的映射设置,这总是很方便:
[Fact]
public void MappingProfile_VerifyMappings()
{
var mappingProfile = new MappingProfile();
var config = new MapperConfiguration(mappingProfile);
var mapper = new Mapper(config);
(mapper as IMapper).ConfigurationProvider.AssertConfigurationIsValid();
}
使用ASP使用AutoMapper。净的核心。
步骤1。从NuGet包中安装AutoMapper.Extensions.Microsoft.DependencyInjection。
步骤2。在“解决方案”中创建一个文件夹以保存名称为“Mappings”的映射。
步骤3。添加映射文件夹后,我们添加了一个名为“MappingProfile”的类,这个名称可以是唯一的,很好理解。
在本课程中,我们将维护所有映射。
步骤4。在启动“ConfigureServices”时初始化映射器
在启动类,我们需要初始化配置文件,我们已经创建和注册AutoMapper服务。
Mapper.Initialize(cfg => cfg.AddProfile<MappingProfile>());
services.AddAutoMapper();
显示ConfigureServices方法的代码片段,其中我们需要初始化和注册AutoMapper。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
// Start Registering and Initializing AutoMapper
Mapper.Initialize(cfg => cfg.AddProfile<MappingProfile>());
services.AddAutoMapper();
// End Registering and Initializing AutoMapper
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}}
第5步。得到的输出。
为了获得映射结果,我们需要调用AutoMapper.Mapper.Map并传递正确的目的地和源。
AutoMapper.Mapper.Map<Destination>(source);
CodeSnippet
[HttpPost]
public void Post([FromBody] SchemeMasterViewModel schemeMaster)
{
if (ModelState.IsValid)
{
var mappedresult = AutoMapper.Mapper.Map<SchemeMaster>(schemeMaster);
}
}
添加到Arve Systad提到的测试中。如果你像我一样,想要维护utz解决方案中提供的继承结构,你可以像这样设置MapperConfiguration:
var mappingProfile = new MappingProfile();
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(mappingProfile);
});
var mapper = new Mapper(config);
我在NUnit中做过这个。
Asp。Net Core 2.2与AutoMapper.Extensions.Microsoft.DependencyInjection。
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Domain, DomainDto>();
}
}
在Startup.cs
services.AddAutoMapper(typeof(List.Handler));
在最新版本的asp.net core中,你应该使用以下初始化:
services.AddAutoMapper(typeof(YourMappingProfileClass));
推荐文章
- HTTP POST返回错误:417“期望失败。”
- 如何在。net中创建和使用资源
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- 如何在c#中获得正确的时间戳
- Linq选择列表中存在的对象(A,B,C)
- c# .NET中的App.config是什么?如何使用它?
- 在哪里放置AutoMapper.CreateMaps?
- c#:如何获得一个字符串的第一个字符?
- String类中的什么方法只返回前N个字符?
- 更好的方法将对象转换为int类型
- 我可以将c#字符串值转换为转义字符串文字吗?
- 在c#中转换char到int
- c#中朋友的对等物是什么?
- 关键字使用virtual+override vs. new
- .NET核心MVC页面更改后不刷新