tijian_tieying/web/dccdc/Common/JsonResultDate.cs
2025-02-20 12:14:39 +08:00

44 lines
1.5 KiB
C#

using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace dccdc.Common
{
public class JsonResultDate : JsonResult
{
const string error = "请设置JsonRequestBehavior AllowGet。";
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException(error);
}
System.Web.HttpResponseBase response = context.HttpContext.Response;
if (!string.IsNullOrEmpty(this.ContentType))
{
response.ContentType = this.ContentType;
}
else
{
response.ContentType = "application/json";
}
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
if (this.Data != null)
{
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" };
string result = Newtonsoft.Json.JsonConvert.SerializeObject(Data, Newtonsoft.Json.Formatting.Indented, timeConverter);
response.Write(result);
}
}
}
}