3

I had a problem regarding MaxJsonLength in MVC. The problem occurred when i returned json(). Then i found a solution here (please read this) answered by fanisch. Now i have many controllers where i have problem of MaxJsonLength. I want to override this method globally.

protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonResult()
    {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding,
        JsonRequestBehavior = behavior,
        MaxJsonLength = Int32.MaxValue
    };
}

How can i do this? Is there any way to impliment this method globally or should i use action filters?

Community
  • 1
  • 1
Umer Waheed
  • 4,044
  • 7
  • 41
  • 62
  • 2
    Make all your controllers inherit from this (base) controller. – haim770 Sep 20 '16 at 12:34
  • Yes, I thought about it but is there any other way without inheirt from base controller. – Umer Waheed Sep 20 '16 at 12:35
  • You could also probably use a global action filter to inspect the result on the way out of the pipeline. (crosscutting concern) – Nkosi Sep 20 '16 at 12:39
  • You can create an extension method to do that. For example, instead of `return Json(data)` you do `return data.AsJson()`. Otherwise, you'll have to opt for a global filter. – haim770 Sep 20 '16 at 12:43
  • @haim770 thanks for your comment. – Umer Waheed Sep 20 '16 at 12:56
  • @Nkosi thanks for your comment. – Umer Waheed Sep 20 '16 at 12:56
  • I am going to try extension method and then global filter. – Umer Waheed Sep 20 '16 at 12:57
  • @Umer, just so you know. using the extension method would still require you to have to manually edit all the controllers or use inheritance. – Nkosi Sep 20 '16 at 12:58
  • Actually i have never tried extension method. BTW thanks for telling me. – Umer Waheed Sep 20 '16 at 13:00
  • @Nkosi that's incorrect, you could just extend the Controller class and its available to all your controllers. That said I think an action filter is actually the best way to go here. – Paul Swetz Sep 20 '16 at 13:46
  • Well, if you just need one method there is no real need for architectural changes, just create extension method. – Fabjan Sep 20 '16 at 15:15
  • Possible duplicate of [Can I set an unlimited length for maxJsonLength in web.config?](http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config) – DavidG Sep 20 '16 at 15:41

1 Answers1

3

The easiest way to to create an extension method (totally in agreement with other comments on the OP that said the same). Here is an implementation of your method in your OP as an extension method. You can rename it as you see fit. I also added some defaults to the parameters which are the same as those used in the method overloads of the controller.

public static class ControllerExtensions {
    public static JsonResult AsJson(this Controller controller, object data, JsonRequestBehavior behavior = JsonRequestBehavior.AllowGet, string contentType = null, System.Text.Encoding contentEncoding = null)
    {
        return new JsonResult()
        {
            Data = data,
            ContentType = contentType,
            ContentEncoding = contentEncoding,
            JsonRequestBehavior = behavior,
            MaxJsonLength = Int32.MaxValue
        };
    }
}

// how to call from inside an action (method) on a controller
public class SomeController : Controller {
    public JsonResult GetSomething(){
        return this.AsJson(new {prop1 = "testing"});
    }
}

For more on extension methods see Extension Methods

Igor
  • 60,821
  • 10
  • 100
  • 175