问题
UPDATE: solved! Nothing to see here, please move on :-)
I have an ApiController
method that takes a System.Version
parameter. The parameter is passed in the request body, as JSON. This is what gets sent:
{ "Major": 0, "Minor": 7, "Build": 0, "Revision": 0, "MajorRevision": 0, "MinorRevision": 0 }
The routing works - my method is being called - but the parameter has an empty Version
object (all values zero). Why?
Here's the declaration of the controller method:
// POST api/service/details
[HttpPost]
[ActionName("Details")]
public ServiceDto Get(Version version)
{
}
回答1:
To prevent anyone else from trying to answer this, the answer is that the System.Version class has no public setters on its members. Hence it cannot be de-serialized properly, and remains in its initial state. I had to use my own "Version" class that had publicly-settable members, and that worked fine.
回答2:
I dont think [Serializable] could be an issue.
Try putting 'Content-Type: application/json' as header from the client side. Also use [FromBody] attribute like this.
[HttpPost]
[ActionName("Details")]
public ServiceDto Get([FromBody]Version version)
{
}
来源:https://stackoverflow.com/questions/12592746/why-does-webapi-fail-to-bind-a-system-version-parameter