JsonRequestBehaviour.AllowGet & Visual Studio Regex

Nice quick win – currently upgrading a client Intranet project from ASP.NET MVC1 to MVC2. Microsoft changed how get requests are handled in this release and disallowed GET JSON requests by default. Before going any further, note that there are security implications in allowing JSON get requests.

To get around this, either make your JSON requests happen by POST, or change:

return Json(object);

to

return Json(object, JsonRequestBehaviour.AllowGet); 

Not too bad if you’ve only got a couple of instances, but if you’ve got Json coming out of your ears, it’s a pain to implement it everywhere. You can either derive yourself a controller object and override the default Json behaviour – or use a Visual Studio find and replace regex to change your Json responses for you:

Unlike our Ruby, PHPing and Perl counterparts, regular expressions aren’t something that .NET developers tend to come across on a regular basis, and as such we often forget they’re lurking in the toolbox. Visual Studio find/replace has a reasonable regex mechanism, but it’s not perl standard. Check the docs for details.

For reference – we know this isn’t best practice, but if you need a quick and dirty fix, this will do it.