MVC 5 Increase Max JSON Length in POST Request

前端 未结 2 1759
梦毁少年i
梦毁少年i 2020-12-11 05:21

I am sending a POST request to an MVC controller with a large amount of JSON data in the body and it is throwing the following:

ArgumentException: Err

相关标签:
2条回答
  • 2020-12-11 05:35
    <system.web>
        <httpRuntime  maxRequestLength="1048576" />  
    </system.web>
    
    <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="1073741824" />
          </requestFiltering>
        </security>
    </system.webServer>
    
    0 讨论(0)
  • 2020-12-11 05:42

    So, although this is a rather disagreeable solution, I got around the problem by reading the request stream manually, rather than relying on MVC's model binders.

    For example, my method

    [HttpPost]
    public JsonResult MyMethod (string data = "") { //... }
    

    Became

    [HttpPost]
    public JsonResult MyMethod () {
        Stream req = Request.InputStream;
        req.Seek(0, System.IO.SeekOrigin.Begin);
        string json = new StreamReader(req).ReadToEnd();
        MyModel model = JsonConvert.DeserializeObject<MyModel>(json);
        // use model...
    }
    

    This way I could use JSON.NET and get around the JSON max length restrictions with MVC's default deserializer.

    To adapt this solution, I would recommend creating a custom JsonResult factory that will replace the old in Application_Start().

    0 讨论(0)
提交回复
热议问题