C#Web api从JSON获取POST数据

时间:2018-03-22 19:54:29

标签: c# json http post asp.net-web-api

我有一个问题,我一直试图解决。我试图将数据从java应用程序发送到Web服务器,但我无法弄清楚如何实际发送它。 java代码如下:

String hStr = "{\"id\":2,\"name\":\"John\",\"height\":36.72342538,\"width\":2.99999998,\"frequency\":871.07,\\"idList\":[],\"level\":0.0}";

House ap = toJsonMap.readValue(hStr, House.class);
when: "ask the server to add a house from the request"
def response = server.httpClient.requestSpec { spec ->
    spec.body { b -> 
    b.text(hStr) 
    b.type("application/json")
    }
} 
.post("//modeling/housing/{hid}/prop/point/in");

然后我让C#读取这样的代码:

    [Route("modeling/housing/{hid}/prop/point/in")]
    [HttpPost]
    public HttpResponseMessage AddPoint(int hid, int id, string name, double height, double width, double frequency, List<int> idList, double level)
    {
        DAL.House h = new DAL.House();

        try
        {
            using (DAL.Entities context = DAL.Entities.CreateContextForComplex(said))
            {
                if (!context.Houses.Where(a => a.Id == id).Any())
                {

                    h.Name = name;
                    h.Height = height;
                    h.Width = width;
                    h.Frequency = frequency;
                    h.IdList= idList;
                    h.Level = level;
                    h.LastModified = System.DateTime.UtcNow;

                    context.Houses.Add(ap);
                    context.SaveChanges();

                    return Request.CreateResponse(HttpStatusCode.OK, ap);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, "Housing id already exists");
                }
            }
        }
        catch (EntityException)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError, "Entity Exception");
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
        }
    }

我无法弄清楚如何从这篇文章中获取数据。特别是获得所有不同类型的变量。我发现了许多不同的答案,但似乎没有任何效果。

3 个答案:

答案 0 :(得分:2)

您很可能需要创建一个具有与传入请求帖子正文的对象属性匹配的属性的类。例如:

public class House
{
  public int Hid { get; set; }
  public int Id { get; set; }
  public string Name { get; set; }
  public double Height { get; set; }
  public double Width { get; set; }
  public double Frequency { get; set; }
  public List<int> IdList { get; set; }
  public double Level { get; set; }
}

然后,您将按如下方式更新方法签名:

public HttpResponseMessage AddPoint(House house)

答案 1 :(得分:2)

尝试创建一个表示JSON对象中所有属性的类:

public class YouClass
{ 
    public int Id { get; set; }
    public string Name { get; set; }
    public string Height { get; set; }
 ......
    // add others
 }

然后在你的控制器中:

public class HousingController : ApiController
 {
    [Route("AddPoint")
    [HttpPost]
    public HttpResponseMessage AddPoint([FromBody] YourClass)
    {

    }
}

然后修改您正在调用的API的URL:

.post("api/Housing/Addpoint")

您的网址可能不同,您可以使用:http://localhost:Port/api/Housing/Addpoint和端口。确保首先在浏览器中尝试或使用Postman。查看this

答案 2 :(得分:0)

.post("//modeling/housing/{hid}/prop/point/in");

这行代码应该在java中给你一个超时,如果这正是你输入它的方式。你真正想要的更像是:

.post("http://localhost:PortNumber/modeling/housing/"+ ap.id +"/prop/point/in");

其中PortNumber是运行web api的端口,而ap.Id是您尝试修改的记录的ID。

更正端点情况后,继续查看其他答案并使用JSON.Net将JSON反序列化为类。