簡體   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