Skip to content

Instantly share code, notes, and snippets.

@hanssens
Last active August 29, 2015 14:08
Show Gist options
  • Save hanssens/4805fc23a1c9e7716910 to your computer and use it in GitHub Desktop.
Save hanssens/4805fc23a1c9e7716910 to your computer and use it in GitHub Desktop.
JSONP FilterAttribute for ASP.NET MVC
public class JsonpFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(
ActionExecutedContext filterContext)
{
if (filterContext == null)
throw new ArgumentNullException("filterContext");
//
// see if this request included a "callback" querystring
// parameter
//
string callback = filterContext.HttpContext.
Request.QueryString["callback"];
if (callback != null && callback.Length > 0)
{
//
// ensure that the result is a "JsonResult"
//
JsonResult result = filterContext.Result as JsonResult;
if (result == null)
{
throw new InvalidOperationException(
"JsonpFilterAttribute must be applied only " +
"on controllers and actions that return a " +
"JsonResult object.");
}
filterContext.Result = new JsonpResult
{
ContentEncoding = result.ContentEncoding,
ContentType = result.ContentType,
Data = result.Data,
Callback = callback
};
}
}
}
public class JsonpResult : JsonResult
{
/// <summary>
/// Gets or sets the javascript callback function that is
/// to be invoked in the resulting script output.
/// </summary>
/// <value>The callback function name.</value>
public string Callback { get; set; }
/// <summary>
/// Enables processing of the result of an action method by a
/// custom type that inherits from
/// <see cref="T:System.Web.Mvc.ActionResult"/>.
/// </summary>
/// <param name="context">The context within which the
/// result is executed.</param>
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
response.ContentType = ContentType;
else
response.ContentType = "application/javascript";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Callback == null || Callback.Length == 0)
{
Callback = context.HttpContext.
Request.QueryString["callback"];
}
if (Data != null)
{
// The JavaScriptSerializer type was marked as obsolete
// prior to .NET Framework 3.5 SP1
#pragma warning disable 0618
JavaScriptSerializer serializer =
new JavaScriptSerializer();
string ser = serializer.Serialize(Data);
response.Write(Callback + "(" + ser + ");");
#pragma warning restore 0618
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment