在ASP.NET WebAPI中潜水,我将HTTP Post / Put发送到服务器,但没有返回任何值,看起来WebApi无法识别所提供的值。
为什么我无法获得返回的值?我错过了什么?
我有这个"复杂"类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CoreAPI.DB.Tables;
namespace CoreAPI.Controllers.RequestObjects
{
public class CompanyBuildingObject
{
public int Id;
public int Company_Id;
public int Building_Id;
public int[] BuildingUnit_Id;
public CompanyBuildingObject()
{
}
}
}
这是控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using CoreAPI.DB.Tables;
using CoreAPI.Controllers.RequestObjects;
namespace CoreAPI.Controllers
{
public class CompanyBuildingsController : ApiController
{
private ApplicationDatabase db = new ApplicationDatabase();
}
[HttpPut]
[Route("api/CompanyBuildings/PutSyncCompanyBuildingUnits/{id}")]
[ResponseType(typeof(CompanyBuildingObject))]
public IHttpActionResult PutSynCompanyBuildingUnits(int id, CompanyBuildingObject companyBuildingObject)
{
return Ok(companyBuildingObject);
}
[HttpPost]
[Route("api/CompanyBuildings/PostSyncCompanyBuildingUnits/{id}")]
[ResponseType(typeof(CompanyBuildingObject))]
public IHttpActionResult PostSynCompanyBuildingUnits(int id, CompanyBuildingObject companyBuildingObject)
{
return Ok(companyBuildingObject);
}
}
这是邮差测试
答案 0 :(得分:0)
如果您发送的是VIA Json。您的格式应如下所示。
{
"id": 3634,
"CompanyBuildingObject":{
"Id":1,
"Company_Id":2,
"Building_Id":2,
"BuildingUnit_Id":[1,2,3,4]
}
}
尝试POST Man或任何客户将数据发布到您的API。
而不是form-urlencoded try" RAW"使用json
格式
答案 1 :(得分:0)
我已经测试了用于读取/写入私有字段的属性的实现。现在正在工作,这是目前的课程。
public class CompanyBuildingObject
{
private int _CompanyId = 0;
private int _BuildingId = 0;
private int _BuildingUnitId = 0;
public int Company_Id
{
get { return _CompanyId; }
set { _CompanyId = value; }
}
public int Building_Id
{
get { return _BuildingId; }
set { _BuildingId = value; }
}
public int BuildingUnit_Id
{
get { return _BuildingUnitId; }
set { _BuildingUnitId = value; }
}
public CompanyBuildingObject()
{
Company_Id = 0;
Building_Id = 0;
BuildingUnit_Id = 0;
}
}
[HttpPut]
[ResponseType(typeof(CompanyBuildingUnit))]
[Route("api/CompanyBuildingUnits/PutCompanyBuildingUnit/{id}")]
public IHttpActionResult PutCompanyBuildingUnit(int id, CompanyBuildingObject cbo)
{
return Ok(SyncCompanyBuildingUnits(id, cbo));
}
[HttpPost]
[ResponseType(typeof(CompanyBuildingUnit))]
[Route("api/CompanyBuildingUnits/PostCompanyBuildingUnit/")]
public IHttpActionResult PostCompanyBuildingUnit(CompanyBuildingObject cbo)
{
return Ok(SyncCompanyBuildingUnits(0, cbo));
}