发布整数数组asp.net网页API [英] Posting array of integers to asp.net web api

查看:111
本文介绍了发布整数数组asp.net网页API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个低于类张贴到asp.net网页API

 公共类的PostData
{    公众诠释ID {搞定;组; }    公众诠释[] {SelectedChoiceIDs获得;组; }}

在我ApiController有一个方法调用发送的可获得的PostData 对象作为参数。<​​/ P>

 公共BOOL发送(数据的PostData)
{
}

问题是每当我追溯方法的Web API不绑定到整数数组,也就是 SelectedChoiceIDs 属性。我怎么可以强制整数数组绑定 SelectedChoiceIDs 属性?

我要送的数据是像

  {ID:3,SelectedChoiceIDs:[3,4,5,6]}


解决方案

您不需要做任何事情,这将工作开箱。

如果您发布您提供的确切的对象:

  {ID:3,SelectedChoiceIDs:[3,4,5,6]}

内容类型:应用程序/ JSON ,默认的模型绑定会自动把它捡起来。

 公共类的PostData
{    公众诠释ID {搞定;组; }    公众诠释[] {SelectedChoiceIDs获得;组; }}公共无效DummyController:ApiController
{
    公共无效邮报(数据的PostData)
    {
        //数据在这里将用的PostData ID和4个整数数组
    }
}

请确保您提供的内容类型,而且你确实张贴正确的JSON,而不是例如:

  {数据:{ID:3,SelectedChoiceIDs:[3,4,5,6]}}

I have a class like below for posting to asp.net web api

public class PostData
{

    public int ID { get; set; }

    public int[] SelectedChoiceIDs { get; set; }

}

In my ApiController there is a method called send which gets PostData object as parameter.

public bool Send(PostData data)
{


}

The problem is whenever I trace the method Web API is not binding to the array of integers ,that is, SelectedChoiceIDs property. How can i force to bind the array of integers to "SelectedChoiceIDs" property?

The data i'm sending is like

{ "ID" : 3 , "SelectedChoiceIDs" : [ 3,4,5,6 ] } 

解决方案

You don't need to do anything, this will work out of the box.

If you POST the exact object you provided:

{ "ID" : 3 , "SelectedChoiceIDs" : [ 3,4,5,6 ] } 

with Content-Type: application/json, the default model binder will automatically pick it up.

public class PostData
{

    public int ID { get; set; }

    public int[] SelectedChoiceIDs { get; set; }

}

public void DummyController : ApiController
{
    public void Post(PostData data)
    {
        //data here will be PostData with ID and an array of 4 integers
    }
}

Make sure you provide the Content-Type, and that you are indeed posting correct JSON, not for example:

{data: { "ID" : 3 , "SelectedChoiceIDs" : [ 3,4,5,6 ] } }

这篇关于发布整数数组asp.net网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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