Return JSon from an Action (MVC Framework)

I can return the JSon of any object from a common action by using the Json() method of the Controller class.

public class TestController : Controller
{
    public ActionResult GetTestObject()
    {
        return Json(new
        {
            Name = "Test",
            Value = "TestValue"
        }, JsonRequestBehavior.AllowGet);
    }
}

The AllowGet option tell MVC Framework that we want to be able to get this JSon from a get request on this action. Without it we’ll receive a runtime error, because by default MVC Framework won’t return any JSon content for security reasons.

Advertisements

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