Skip to content

Instantly share code, notes, and snippets.

@dax70
Created June 16, 2012 21:57
Show Gist options
  • Save dax70/2942622 to your computer and use it in GitHub Desktop.
Save dax70/2942622 to your computer and use it in GitHub Desktop.
Controller override for Json.NET
/// <summary>
/// Creates a System.Web.Mvc.JsonResult object that serializes the specified object to JavaScript Object Notation (JSON).
/// </summary>
/// <param name="data">The JavaScript object graph to serialize.</param>
/// <param name="contentType">The content type (MIME type).</param>
/// <param name="contentEncoding">The content encoding.</param>
/// <param name="behavior">The JSON request behavior</param>
/// <returns>
/// The JSON result object that serializes the specified object to JSON format.
/// The result object that is prepared by this method is written to the response
/// by the ASP.NET MVC framework when the object is executed.
/// </returns>
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
// No need to override other overloads since they all eventually call this one.
return new JsonNetResult
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior // Technically no longer needed since we handle adding a container.
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment