Preview version of VS2013 new WebAPI project. I got exceptions during modelbinding (i.e. couldn't catch the exception with the debugger), and as far as I can see it comes down to this:
public class SomeModel {
public int SomeID {get; set;}
public int SomeValue {get; set;}
}
[HttpPost("base/{id}/somemodels")]
public HttpResponseMessage Post(int id, SomeModel model)
{
....
}
The idea being to bind the id to the uri segment, and the SomeModel object from the body. Annotations such as [FromUrl] and [FromBody] didn't change anything.
If I changed the properties of the SomeModel object to be strings it works perfectly. If either of them are Int it fails with an error of "Unable to cast object of type 'System.Int32' to type 'System.Array'".
A custom modelbinder and valueprovider to pull the object from the body content fixed it for me, but it should work out of the box, right?
Finally, as an addition annoyance the custom modelbinder breaks my help page sample generation :-(
Complete error:
{"Message":"An error has occurred.","ExceptionMessage":"Unable to cast object of type 'System.Int32' to type 'System.Array'.","ExceptionType":"System.InvalidCastException","StackTrace":" at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__b.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"}
Comments: I'm having some trouble deploying the code to azure websites, but as soon as I get it working I'll add a link.
public class SomeModel {
public int SomeID {get; set;}
public int SomeValue {get; set;}
}
[HttpPost("base/{id}/somemodels")]
public HttpResponseMessage Post(int id, SomeModel model)
{
....
}
The idea being to bind the id to the uri segment, and the SomeModel object from the body. Annotations such as [FromUrl] and [FromBody] didn't change anything.
If I changed the properties of the SomeModel object to be strings it works perfectly. If either of them are Int it fails with an error of "Unable to cast object of type 'System.Int32' to type 'System.Array'".
A custom modelbinder and valueprovider to pull the object from the body content fixed it for me, but it should work out of the box, right?
Finally, as an addition annoyance the custom modelbinder breaks my help page sample generation :-(
Complete error:
{"Message":"An error has occurred.","ExceptionMessage":"Unable to cast object of type 'System.Int32' to type 'System.Array'.","ExceptionType":"System.InvalidCastException","StackTrace":" at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__b.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"}
Comments: I'm having some trouble deploying the code to azure websites, but as soon as I get it working I'll add a link.