ASP.NET JsonResult返回日期格式及首字母大写解决
2021-04-30 02:27
标签:form time code serialize tip throw pre void vat 添加一个类继承JsonResult 调用: ASP.NET JsonResult返回日期格式及首字母大写解决 标签:form time code serialize tip throw pre void vat 原文地址:https://www.cnblogs.com/Zingu/p/14711100.html public class CustomJsonResult : JsonResult
{
private const string _dateFormat = "yyyy-MM-dd HH:mm:ss";
public CustomJsonResult()
{
serializerSettings = new JsonSerializerSettings
{
// 设置为驼峰命名
ContractResolver = new CamelCasePropertyNamesContractResolver(), //首字母小写
DateFormatString = _dateFormat //日期格式
};
}
private JsonSerializerSettings serializerSettings { get; set; }
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/json";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Data != null)
{
// Using Json.NET serializer
var sctiptSerialize = JsonSerializer.Create(serializerSettings);
sctiptSerialize.Serialize(response.Output, Data);
}
}
public ActionResult Test()
{
Person p = new Person() { Name = "zhangsan", Age = 1, BirthDay = DateTime.Now };
//return Json(d);
return new CustomJsonResult {Data=p };
}
上一篇:jQuery练习之图片跟随
下一篇:二次封装上传element
文章标题:ASP.NET JsonResult返回日期格式及首字母大写解决
文章链接:http://soscw.com/index.php/essay/80239.html