【问题标题】:C# Web api getting POST data from JSONC# Web api 从 JSON 获取 POST 数据
【发布时间】: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


【解决方案1】:

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

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 字符串,这会起作用吗?或者这就是字符串被分解的具体方式?
  • @mightynifty 它应该。不过,您在上面发布的 JSON 中似乎确实存在错误(idList 之前的双 \\)。
  • 您不应该将属性 FromBody 添加到您的方法签名中以使其工作吗?
  • @KevinAvignon 仅当您使用 Asp.Net Core 时。见documentation。另请注意,对于下一个核心版本,this will go away as well
【解决方案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")

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

【讨论】:

  • 他是从 Java 应用程序发送的。他不必包括"http://localhost:Port/Addpoint"吗?
  • 是的,他应该取决于 api 的 URL
【解决方案3】:
.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 反序列化回一个类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2012-10-24
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多