Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mc.AddProfile(new DtoToOtherProfile(new HttpContextAccessor()));
- //OR
- var mapperConfiguration = new MapperConfiguration(mc =>
- {
- IServiceProvider provider = services.BuildServiceProvider();
- mc.AddProfile(new DtoToOtherProfile(provider.GetService<IHttpContextAccessor>()));
- });
- public DtoToOtherProfile(IHttpContextAccessor httpContextAccessor)
- {
- _httpContextAccessor = httpContextAccessor;
- CreateMap<MyDto, MyOther>()
- .ConstructUsing(myDto => new myOther(myDto.prop1, myDto .prop2, _httpContextAccessor.HttpContext.User.Identity.Name)); // the name is what i intend to pass
- // other mapping
- }
- public async Task<IActionResult> Create([FromBody]MyDto model)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- // save campaign
- var myOtherModel = _mapper.Map<MyOther>(model);
- // myOtherModel.UserName is always null
- // rest
- }
- services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
- IServiceProvider provider = services.BuildServiceProvider();
- var mapperConfiguration = new MapperConfiguration(mc =>
- {
- mc.AddProfile(new DtoToOtherProfile(provider.GetRequiredService<IHttpContextAccessor>()));
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement