Created
March 21, 2014 04:43
-
-
Save ducas/9679655 to your computer and use it in GitHub Desktop.
MVC + JSend Result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global alert */ | |
define( | |
['jquery'], | |
function ($) { | |
$.ajaxPrefilter(function (options, originalOptions, jqXHR) { | |
jqXHR.then(function (data) { | |
if (!data) { | |
return; | |
} | |
if (data.status !== 'error') { | |
return; | |
} | |
var message = "Oops... something went wrong!"; | |
if (!!data.message) { | |
message += "\r\n\r\n" + data.message; | |
} | |
alert(message); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Mvc; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Converters; | |
using Newtonsoft.Json.Serialization; | |
namespace Web.Core | |
{ | |
public class JSendResult : JsonResult | |
{ | |
public static JSendResult Success(object data) | |
{ | |
return new JSendResult(new Payload | |
{ | |
Data = data, | |
Status = Status.Success | |
}); | |
} | |
public static JSendResult Fail(object data) | |
{ | |
return new JSendResult(new Payload | |
{ | |
Data = data, | |
Status = Status.Fail | |
}); | |
} | |
public static JSendResult Error(string message, object data = null, int? code = null) | |
{ | |
return new JSendResult(new Payload | |
{ | |
Data = data, | |
Status = Status.Error | |
}); | |
} | |
private JSendResult(Payload data) | |
{ | |
Data = data; | |
JsonRequestBehavior = JsonRequestBehavior.AllowGet; | |
} | |
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings | |
{ | |
ContractResolver = new CamelCasePropertyNamesContractResolver() | |
}; | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
if (context == null) | |
throw new ArgumentNullException("context"); | |
var response = context.HttpContext.Response; | |
response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/json" : ContentType; | |
if (ContentEncoding != null) | |
response.ContentEncoding = ContentEncoding; | |
if (Data == null) | |
return; | |
response.Write(JsonConvert.SerializeObject(Data, JsonSerializerSettings)); | |
} | |
public class Payload | |
{ | |
[JsonConverter(typeof(CamelCaseStringEnumConverter))] | |
public Status Status { get; set; } | |
public object Data { get; set; } | |
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)] | |
public string Message { get; set; } | |
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)] | |
public int? Code { get; set; } | |
} | |
public enum Status | |
{ | |
Success, | |
Fail, | |
Error | |
} | |
public class CamelCaseStringEnumConverter : StringEnumConverter | |
{ | |
public CamelCaseStringEnumConverter() | |
{ | |
CamelCaseText = true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment