Skip to content

Instantly share code, notes, and snippets.

@jakejscott
Created June 21, 2011 09:32
Show Gist options
  • Save jakejscott/1037528 to your computer and use it in GitHub Desktop.
Save jakejscott/1037528 to your computer and use it in GitHub Desktop.
Override asp.net mvc JsonResult with the fast ServiceStack JsonSerializer
public abstract class ApplicationController : Controller
{
protected ActionResult RespondTo(Action<FormatCollection> format) {
return new FormatResult(format);
}
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) {
return new ServiceStackJsonResult {
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding
};
}
}
public class ServiceStackJsonResult : JsonResult
{
public override void ExecuteResult(ControllerContext context) {
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
if (ContentEncoding != null) {
response.ContentEncoding = ContentEncoding;
}
if (Data != null) {
response.Write(JsonSerializer.SerializeToString(Data));
}
}
}
@yhersh
Copy link

yhersh commented Sep 23, 2011

Looks great,
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment