How to receive json in ASP.NET web api?

后端 未结 3 1474
北海茫月
北海茫月 2020-12-18 08:51

I have an external company pushing data to one of our server, they are going to send JSON data. I need to create a POST api to receive it. This is what I have so far

3条回答
  •  忘掉有多难
    2020-12-18 09:32

    Your Web API model needs to be same as javascript request object

    For example,

    public class SomeRequest
        {
    
            public string data1{ get; set; }
            public string data2{ get; set; }
            public string data13{ get; set; }
    
        }
    

    In Javascript , your request object and javascript call should be like this

     var requestObj = {
                    "data1": data1,
                    "data2": data2,
                    "data3": data3,
    
                };
    

    And post url and requestObj in javascript

    Access in Web API like this

      public void PushSensorData(SomeRequest objectRequest)
            {
    objectRequest.data1
    objectRequest.data2
    objectRequest.data3
    

提交回复
热议问题