MVC 5 Increase Max JSON Length in POST Request

前端 未结 2 1765
梦毁少年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: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(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().

提交回复
热议问题