可以創建自定義jsonresult
public class CustomJsonResult : JsonResult
{
private const string _dateFormat = "yyyy-MM-dd HH:mm:ss";
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 isoConvert = new IsoDateTimeConverter();
isoConvert.DateTimeFormat = _dateFormat;
response.Write(JsonConvert.SerializeObject(Data, isoConvert));
}
}
}
用法示例:
[HttpGet]
public ActionResult Index() {
return new CustomJsonResult { Data = new { } };
}
您還可以創建自定義MediaTypeFormatter格式化和序列化過程中你可以改變日期時間的格式。
更新
Json.Net支持格式的日期,如果你從一個Ajax消耗它叫你可能想看看JavaScriptDateTimeConverter
的多種方式。
Serializing Dates in JSON
只爲你正在使用JSON response.I意味着.NET,JAVA等,這技術澄清.. –
對此深感抱歉,我使用Ajax來獲取返回的JSON – Chris
在其技術後臺寫 –