Web API Get Method with Complex Object as Parameter

后端 未结 3 2030
情话喂你
情话喂你 2021-01-13 07:59

How to send a Complex Object( Which may have huge data) to a Web API Get Method? I know I can user \'FromUri\' option, but since the data is too large I can\'t use that opti

3条回答
  •  既然无缘
    2021-01-13 08:22

    You need to create a method similar to the below:

    [HttpPost]
    public HttpResponseMessage MyMethod([FromBody] MyComplexObject body)
    {
        try
        {
            // Do stuff with 'body'
            myService.Process(body);
        }
        catch (MyCustomException)
        {
            return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("FAILED") };
        }
    
        return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("OK") };
    }
    

    If your POSTing data larger than 4MB, then you'll need to adjust your web.config to configure IIS to accept more, the below example sets the maxRequestLength and maxAllowedContentLength to 10MB:

    
        
    
    

    and

     
           
               
                  
               
           
    
    

提交回复
热议问题