Why does WebApi fail to bind a System.Version parameter?

好久不见. 提交于 2020-01-02 09:58:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!