Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- [Route("api/post/")]
- public IHttpActionResult MyPost([ModelBinder(typeof(MyModelBinder))]IModel viewModel)
- {
- doSomething(viewModel);
- return Ok("ok");
- }
- public class MyModelBinder : IModelBinder
- {
- public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
- {
- var content = actionContext.Request.Content;
- string json = content.ReadAsStringAsync().Result;
- try
- {
- var DTO = JsonConvert.DeserializeObject<A>(json);
- IModel viewModel = new A()
- {
- // mapping here
- };
- bindingContext.Model = viewModel;
- return true;
- }
- catch(Exception e)
- {
- }
- try
- {
- var DTO = JsonConvert.DeserializeObject<B>(json);
- IModel viewModel = new B()
- {
- // mapping here
- };
- bindingContext.Model = viewModel;
- return true;
- }
- catch (Exception e)
- {
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment