【发布时间】:2018-09-01 09:40:47
【问题描述】:
我有一个问题一直在尝试解决。我正在尝试将数据从 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);
}
}
我只是不知道如何从这篇文章中获取数据。特别是获取所有不同类型的变量。我找到了很多不同的答案,但似乎没有任何效果。
【问题讨论】:
-
理想情况下,您的控制器将接受一个参数,该参数是具有映射到 json 的属性的对象。
标签: c# json http post asp.net-web-api