Skip to content

Instantly share code, notes, and snippets.

@juristr
Created December 19, 2012 06:51
Show Gist options
  • Save juristr/4334924 to your computer and use it in GitHub Desktop.
Save juristr/4334924 to your computer and use it in GitHub Desktop.
A simple JsonpResult object for being used in ASP.net MVC applications

Usage

Simply wrap the object to be returned, like

public JsonResult SomeControllerActionMethod()
{
  ...
  return JsonpResult(dataObject);
}

The code has been taken from the book "Programming ASP.net MVC4"

using System.Web.Mvc;
public class JsonpResult : JsonResult
{
public string Callback { get; set; }
public JsonpResult()
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
public override void ExecuteResult(ControllerContext context)
{
var httpContext = context.HttpContext; var callback = Callback;
if(string.IsNullOrWhiteSpace(callback)) callback = httpContext.Request["callback"];
httpContext.Response.Write(callback + "("); base.ExecuteResult(context); httpContext.Response.Write(");");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment