Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. mc.AddProfile(new DtoToOtherProfile(new HttpContextAccessor()));
  2.  
  3. //OR
  4.  
  5. var mapperConfiguration = new MapperConfiguration(mc =>
  6. {
  7. IServiceProvider provider = services.BuildServiceProvider();
  8. mc.AddProfile(new DtoToOtherProfile(provider.GetService<IHttpContextAccessor>()));
  9. });
  10.  
  11. public DtoToOtherProfile(IHttpContextAccessor httpContextAccessor)
  12. {
  13. _httpContextAccessor = httpContextAccessor;
  14.  
  15. CreateMap<MyDto, MyOther>()
  16. .ConstructUsing(myDto => new myOther(myDto.prop1, myDto .prop2, _httpContextAccessor.HttpContext.User.Identity.Name)); // the name is what i intend to pass
  17. // other mapping
  18. }
  19.  
  20. public async Task<IActionResult> Create([FromBody]MyDto model)
  21. {
  22. if (!ModelState.IsValid)
  23. {
  24. return BadRequest(ModelState);
  25. }
  26.  
  27. // save campaign
  28. var myOtherModel = _mapper.Map<MyOther>(model);
  29. // myOtherModel.UserName is always null
  30. // rest
  31. }
  32.  
  33. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  34.  
  35. IServiceProvider provider = services.BuildServiceProvider();
  36.  
  37. var mapperConfiguration = new MapperConfiguration(mc =>
  38. {
  39. mc.AddProfile(new DtoToOtherProfile(provider.GetRequiredService<IHttpContextAccessor>()));
  40. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement