Skip to content

Instantly share code, notes, and snippets.

@fabiomaulo
Created October 13, 2018 19:32
Show Gist options
  • Save fabiomaulo/25ed614add526ceb456b1423e8100c11 to your computer and use it in GitHub Desktop.
Save fabiomaulo/25ed614add526ceb456b1423e8100c11 to your computer and use it in GitHub Desktop.
Jsonp Action Result
public class JsonpResult : JsonResult
{
private const string NullCallbackExceptionMessage = "Callback cannot be null.";
private const string NullContextExceptionMessage = "Context cannot be null.";
private const string JsonpCallbackFormat = "{0}({1});";
private const string JsonpContentType = "application/javascript";
private const string InvalidOperationExceptionMessage =
"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.";
private const string HttpVerbGet = "get";
#region Factories
public static JsonpResult For(string callback, object data)
{
return For(callback, data, null);
}
public static JsonpResult For(string callback, object data, JsonRequestBehavior jsonRequestBehavior)
{
return For(callback, data, null, jsonRequestBehavior);
}
public static JsonpResult For(string callback, object data, string contentType)
{
return For(callback, data, contentType, null);
}
public static JsonpResult For(string callback, object data, string contentType, JsonRequestBehavior jsonRequestBehavior)
{
return For(callback, data, contentType, null, jsonRequestBehavior);
}
public static JsonpResult For(string callback, object data, string contentType, Encoding contentEncoding)
{
return For(callback, data, contentType, contentEncoding, JsonRequestBehavior.DenyGet);
}
public static JsonpResult For(string callback, object data, string contentType, Encoding contentEncoding, JsonRequestBehavior jsonRequestBehavior)
{
return new JsonpResult {Callback = callback, ContentEncoding = contentEncoding, ContentType = contentType, Data = data, JsonRequestBehavior = jsonRequestBehavior};
}
#endregion
public string Callback { get; set; }
public override void ExecuteResult(ControllerContext context)
{
CheckContext(context);
CheckRequestBehavior(context);
CheckCallback();
HttpResponseBase response = context.HttpContext.Response;
SetResponseContentType(response);
SetResponseContentEncoding(response);
string serializedData = string.Empty;
if (Data != null)
{
serializedData = JsonConvert.SerializeObject(Data);
}
response.Write(string.Format(JsonpCallbackFormat, Callback, serializedData));
}
private static void CheckContext(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException(NullContextExceptionMessage);
}
}
private void CheckRequestBehavior(ControllerContext context)
{
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && context.HttpContext.Request.HttpMethod.ToLower() == HttpVerbGet)
{
throw new InvalidOperationException(InvalidOperationExceptionMessage);
}
}
private void CheckCallback()
{
if (Callback == null)
{
throw new ArgumentNullException(NullCallbackExceptionMessage);
}
}
private void SetResponseContentType(HttpResponseBase response)
{
if (string.IsNullOrEmpty(ContentType))
{
response.ContentType = JsonpContentType;
}
else
{
response.ContentType = ContentType;
}
}
private void SetResponseContentEncoding(HttpResponseBase response)
{
if (ContentEncoding == null)
{
return;
}
response.ContentEncoding = ContentEncoding;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment