をチェックアウトし、その後、Newtonsoft.Json
はMVCで返さJsonResult
としてJsonResults
を返すために追加されませんでした以下のようにJavaScriptSerializer
を使用する:この場合
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
}
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)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
if (MaxJsonLength.HasValue)
{
serializer.MaxJsonLength = MaxJsonLength.Value;
}
if (RecursionLimit.HasValue)
{
serializer.RecursionLimit = RecursionLimit.Value;
}
response.Write(serializer.Serialize(Data));
}
}
をWebGrease
がそれに依存しているので、それを添加しました。そして、System.Web.Optimization
にMVCによって提供されたバンドルおよびミニサービスは、WebGrease
に依存しています。
WebApiを持たないデフォルトのMVCアプリには、WebApiではなくバンドルおよびミニネーションサービス用にNewtonsoft.Json
がインストールされます。 System.Web.Http
にWEBAPIによって返さJsonResult
が、それは以下のようにシリアライズだためNewtonsoft.Json
を使用し明確にすることが
:
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace System.Web.Http.Results
{
/// <summary>
/// Represents an action result that returns an <see cref="F:System.Net.HttpStatusCode.OK"/> response with JSON data.
/// </summary>
/// <typeparam name="T">The type of content in the entity body.</typeparam>
public class JsonResult<T> : IHttpActionResult
しかしNewtonsoft.Json
はあなたが決めるかもしれません念の非WEBAPI、デフォルトMVCプロジェクトには含まれていません何らかのWebApiを使用してください。上記のように、WebGrease
が必要です。 vNextで何をやっているのかわからない、たぶんNewtonsoft.Json
。
パフォーマンスと柔軟性についてです。パフォーマンスについては、http://james.newtonking.com/archive/2012/01/23/json-net-4-0-release-6-serialization-performance.aspx –