Вы можете сделать это с помощью ServiceStack JSON serializer, но сначала вы должны интегрировать его в ASP.NET MVC.
После установки пакета, настроить DateTime сериализации в запуске приложения:
JsConfig.DateHandler = JsonDateHandler.ISO8601;
Создать тип ActionResult для содержания JSON:
public class CustomJsonResult : ActionResult
{
private readonly object _data;
private readonly string _content;
private readonly Encoding _encoding;
public CustomJsonResult(object data) : this(data, null, null) { }
public CustomJsonResult(object data, string content) : this(data, content, null) { }
public CustomJsonResult(object data, Encoding encoding) : this(data, null, encoding) { }
public CustomJsonResult(object data, string content, Encoding encoding)
{
_data = data;
_content = content;
_encoding = encoding;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = string.IsNullOrEmpty(_content) ? "application/json" : _content;
if (_encoding != null)
{
response.ContentEncoding = _encoding;
}
response.Write(JsonSerializer.SerializeToString(_data));
}
}
Затем вы можете добавить эти методы базового контроллера:
protected CustomJsonResult CustomJson(object data)
{
return new CustomJsonResult(data);
}
protected CustomJsonResult CustomJson(object data, string content)
{
return new CustomJsonResult(data, content);
}
protected CustomJsonResult CustomJson(object data, Encoding encoding)
{
return new CustomJsonResult(data, encoding);
}
protected CustomJsonResult CustomJson(object data, string content, Encoding encoding)
{
return new CustomJsonResult(data, content, encoding);
}
Наконец-то вы можете вернуть результат следующим образом:
return CustomJson(saleList);
Вы можете использовать сериализатор ServiceStack? –
Я могу использовать любое бесплатное программное обеспечение в C# – Andrus