繁体   English   中英

C#Web API从JSON获取POST数据

[英]C# Web api getting POST data from JSON

我有一个一直在试图解决的问题。 我正在尝试将数据从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);
        }
    }

我只是不知道如何从这篇文章中获取数据。 特别是获取所有不同类型的变量。 我找到了许多不同的答案,但似乎没有任何效果。

您最有可能需要创建一个具有与传入请求发布主体的对象属性匹配的属性的类。 例如:

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)

尝试创建一个代表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")

您的URL可能有所不同,您可以使用: http://localhost:Port/api/Housing/Addpoint和端口。 确保先在浏览器中尝试使用Postman。 检查一下

.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反序列化回一个类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM