tag:blogger.com,1999:blog-8166852.post3030854919003659199..comments2024-02-08T10:01:26.782-07:00Comments on Yakko Warner: MVC, JSON, and DateTime (oh my)Yakko Warnerhttp://www.blogger.com/profile/12276550157896918123noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-8166852.post-74564204210724628092014-08-27T11:22:11.011-06:002014-08-27T11:22:11.011-06:00Note that I found an issue with parameterized rout...Note that I found an issue with parameterized routes. When posting JSON to a controller action where the route includes parameters, the model binder is called on each parameter, and this model binder attempts to bind the posted JSON to the parameters.<br /><br />Example:<br />[HttpPost][Route("Customer/{customerId:int}/Vehicle/{vehicleId:int}/Policy/Create"]<br />public async Task Create(int customerId, int vehicleId, PolicyRequest policyRequest)<br /><br />An error is thrown on the first (of three) calls to BindModel, as it tries to bind the JSON to customerId and throws the error "Error reading integer. Unexpected token: StartObject. Path '', line 1, position 1."<br /><br />FromUri/FromBody attributes don't work here, probably because they're WebAPI attributes that MVC doesn't process. (Or maybe it does process them, but we end up in BindModel anyway -- either way, the problem remains.)<br /><br />I've added some code to the top of BindModel that checks to see if the ValueProvider has assigned a value to the current ModelName (which it will when it resolves the route), and skips it instead of trying to deserialize the JSON.Yakko Warnerhttps://www.blogger.com/profile/12276550157896918123noreply@blogger.com