ASP.NET MVC 2.0 JsonRequestBehavior Global Setting

前端 未结 6 880
轻奢々
轻奢々 2021-02-02 06:50

ASP.NET MVC 2.0 will now, by default, throw an exception when an action attempts to return JSON in response to a GET request. I know this can be overridden on a method by method

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-02 07:30

    This, like other MVC-specific settings, is not settable via Web.config. But you have two options:

    1. Override the Controller.Json(object, string, Encoding) overload to call Json(object, string, Encoding, JsonRequestBehavior), passing JsonRequestBehavior.AllowGet as the last argument. If you want this to apply to all controllers, then do this inside an abstract base controller class, then have all your controllers subclass that abstract class.

    2. Make an extension method MyJson(this Controller, ...) which creates a JsonResult and sets the appropriate properties, then call it from your controller via this.MyJson(...).

提交回复
热议问题