Guest User

Untitled

a guest
Jun 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. [HttpPost]
  2. [Route("api/post/")]
  3. public IHttpActionResult MyPost([ModelBinder(typeof(MyModelBinder))]IModel viewModel)
  4. {
  5. doSomething(viewModel);
  6. return Ok("ok");
  7. }
  8.  
  9. public class MyModelBinder : IModelBinder
  10. {
  11. public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
  12. {
  13. var content = actionContext.Request.Content;
  14. string json = content.ReadAsStringAsync().Result;
  15.  
  16. try
  17. {
  18. var DTO = JsonConvert.DeserializeObject<A>(json);
  19. IModel viewModel = new A()
  20. {
  21. // mapping here
  22. };
  23. bindingContext.Model = viewModel;
  24. return true;
  25. }
  26. catch(Exception e)
  27. {
  28.  
  29. }
  30. try
  31. {
  32. var DTO = JsonConvert.DeserializeObject<B>(json);
  33. IModel viewModel = new B()
  34. {
  35. // mapping here
  36. };
  37. bindingContext.Model = viewModel;
  38. return true;
  39. }
  40. catch (Exception e)
  41. {
  42.  
  43. }
  44. return false;
  45. }
  46. }
Add Comment
Please, Sign In to add comment