重写JsonResult 源码转json的方法
public class APIJsonResult : JsonResult
{
public APIJsonResult(int Code, string Message, object Data)
{
code = Code;
message = Message;
data = Data;
}
public int code { get; set; }
public string message { get; set; }
public object data { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
//json转换 换个方法即可
JavaScriptSerializer serializer = new JavaScriptSerializer();
Data = new { code = code, message = message, data = data };
var json= serializer.Serialize(Data);
json = json.Replace("null", "\"\"");
response.Write(json);
}
}