为什么失败的WebAPI绑定一个System.Version参数? [英] Why does WebApi fail to bind a System.Version parameter?

查看:198
本文介绍了为什么失败的WebAPI绑定一个System.Version参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:解决了!没有在这里看到,请移步: - )

我有一个 ApiController 方法,它接受 System.Version 参数。该参数在请求体传递,为JSON。这是被发送:

{
  主要:0,
  未成年人:7,
  建:0,
  修订:0,
  MajorRevision:0,
  MinorRevision:0
}

路由的工作原理 - 我的方法是被称为 - 但参数有一个空的版本对象(所有的值为零)。为什么呢?

下面是在控制器方法的声明:

  // POST API /服务/细节
    [HttpPost]
    [ActionName(详细资料)
    公共ServiceDto获取(版本版)
    {
    }


解决方案

要prevent任何人试图回答这个意外,答案是System.Version类都有其成员没有公开setter方法​​。因此,它不能被正确反序列,并保持在其初始状态。我不得不使用曾公开可设置会员我自己的版本级,而且工作得很好。

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)
    {
    }

解决方案

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.

这篇关于为什么失败的WebAPI绑定一个System.Version参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆