ASP.NET MVC: Internal Server Error (500) on Action Method Returning Json Result


The ASP.NET MVC framework allows you to easily return Json from an action method. This makes jQuery Ajax calls very easy to implement, as shown below.

The JavaScript

$(.button).getJSON('/Home/GetJsonData', 
                   { id = 34 },
                   function(data) {
                      // Do something with it
                   }
);

The Action Method

public ActionResult GetJsonData(int id)
{
     Person person = this.personService.GetPerson(id);     
     return Json(person);
}

There is only one problem with the above action method. If you attempt to run it, the call to the ActionMethod will result in a Internal Server Error (Error 500). The reason is that by default data can only be retrieved using a POST operation if your action method returns a Json result. To make this work with a GET request, all you need to use is the overloaded Json() method shown below.

public ActionResult GetJsonData(int id)
{
     Person person = this.personService.GetPerson(id);     
     return Json(person, JsonRequestBehavior.AllowGet);
}
About these ads

8 Responses to “ASP.NET MVC: Internal Server Error (500) on Action Method Returning Json Result”

  1. behtash Says:

    It Saves a my time,
    Thanks
    Behtash

  2. Henryk Says:

    You rock you saved me so much headache

  3. Ranga Says:

    Thanks i saw this after putting big effort to solve this. Any way this helped me a lot. Thanks.

  4. Rajesh Says:

    Great post. Resolved my issue. Thanks a lot sir.

  5. sahana Says:

    thanks you are awesome

  6. TTTT Says:

    Awesome! Solved my issue

  7. Sergey Says:

    Thank you, really helpful tip!

  8. tarundotnet Says:

    Awesome! Thanks a ton!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: