Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Commented Unassigned: WebAPI modelbinding int and object containing int [1289]

$
0
0
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: Alright, alright - I'm a moron! I had changed a property in the object from string to int, and left in the annotation for [MaxLength()] which is clearly no relevant for an int. In my (slight) defense, the errors and debugging tools were of absolutely no help! :-) I hope this helps anyone else who might stumble across it. Best regards, Martin

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>