如何在web api中发布多个json对象?
我有一个web api控制器,它接受一个Json对象Post(目前使用Postman作为我的用户代理) 以下是我的发帖方法:如何在web api中发布多个json对象?,json,linq,asp.net-web-api,entity-framework-6,Json,Linq,Asp.net Web Api,Entity Framework 6,我有一个web api控制器,它接受一个Json对象Post(目前使用Postman作为我的用户代理) 以下是我的发帖方法: private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities(); [ResponseType(typeof(Sales))] public HttpResponseMessage PostSales(Sales Sales, [FromUri] string auth)
private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();
[ResponseType(typeof(Sales))]
public HttpResponseMessage PostSales(Sales Sales, [FromUri] string auth)
{
try
{
if (ModelState.IsValid)
{
if (auth == "KDI")
{
Int64 rs = db.Sales_.Where(sl => sl.Serial == Sales.Serial).Count();
if (1 == rs)
{
return Request.CreateErrorResponse(HttpStatusCode.Conflict, " Duplicate Found!");
}
else
{
db.Sales_.Add(Sales);
db.SaveChanges();
return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!");
}
}
else
{
return Request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized Access!");
}
}
else
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's wrong with the JSON model you sent me.");
}
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
下面是Json:
{
"ExtSerial": "AH0000002",
"Date": "2015-03-01",
"CustomerRefNbr": "JPM0001",
"Description": "2015 FEBRUARY RENTAL 2015 FEBRUARY RENTAL",
"Customer": "TRDE0065",
"Amount": 17989.51,
"AQ_Branch": "KDI",
"AQ_COA": "4100503000",
"LineSubAccount": "OPMOPN000",
"LineTaxCategory": "JPMTAX",
"LineQuantity": 1,
"LineUnitPrice": 400000,
"AQ_PostStatus": 1,
"AQ_StatusDate": "2015-03-01",
"DTS": "2015-03-01"
}
它工作得很好,但我希望我的post方法可以选择性地接受多个Json数据数组
大概是这样的:
[{"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554}]
{
Sales: { /* the sales object*/ },
IdsList: [ /* the array with the ids list */ ]
}
我尝试将我的销售模型参数作为列表,但它无法读取我的销售实体db.sales\uuuu.Add(sales)
并且它无法在Int64 rs=db.Sales_uu.Where(sl=>sl.serial==Sales.serial.Count())中获取我的销售实体中的表serial代码>
顺便说一句,我正在使用EF6和LINQ。我真的需要您的帮助,我知道这是一个简单的问题,但我真的被卡住了。您不能在Web API POST操作中接收多个JSON对象。这是因为它只能反序列化POST负载一次
唯一的解决方案是发布(客户端)接收(web操作参数)对象,该对象将其他对象作为属性
你的问题不能很好地理解,不过,我会尽量给你一个更具体的答案。您需要将这样的类定义为web api参数:
public class SalesAndIds
{
public Sales Sales { get; set; }
public List<Ids> IdsList { get; set; }
public class Ids
{
public int Id1 { get; set; }
public int Id2 { get; set; }
public int Id3 { get; set; }
}
}
您可以尝试下面的方法,用JArray而不是字符串接收post数据
public <return type> <method name>(string id, [FromBody] JArray value)
{
if (value != null)
{
string data = value.ToString();
}
}
public(字符串id,[FromBody]JArray值)
{
if(值!=null)
{
字符串数据=value.ToString();
}
}
否,在“邮递员”中,您只能撰写请求。为了测试这一点,您需要创建一个POST请求(在下拉列表中将方法更改为POST),然后手工编写JSON负载。为此,请选择“Raw”,然后选择“JSON”类型以获取编辑器帮助来编写JSON负载。不要忘记添加Accept
和Content-Type
标题,这两个标题的值都是:application/json
。有效负载应该看起来像我的答案中的第二个代码块,但是对于延迟的回复,使用的是值而不是注释。这被认为是多个对象吗?{“Id1”:3,“Id2”:76,“Id3”:19},{“Id1”:56,“Id2”:87,“Id3”:94},{“Id1”:976,“Id2”:345,“Id3”:7554}我不能将其发布到我的web api吗?