Se il progetto era solo un progetto MVC senza WebAPI, quindi Newtonsoft.Json
non è stato aggiunto per la restituzione JsonResults
come JsonResult
restituito da MVC utilizza il JavaScriptSerializer
come qui sotto:
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));
}
}
In questo caso è stato aggiunto perché WebGrease
ha una dipendenza da esso. E i servizi di bundling e minification forniti da MVC in System.Web.Optimization
hanno una dipendenza da WebGrease
.
Quindi un'app MVC predefinita senza WebApi avrà Newtonsoft.Json
installato per i servizi di raggruppamento e minificazione non WebApi.
Per essere chiari il JsonResult
restituito da WebAPI in System.Web.Http
non utilizzare Newtonsoft.Json
per la sua serializzazione come di seguito:
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
Ma Newtonsoft.Json
non è incluso in un non WebAPI, progetto MVC solo nel caso in cui si potrebbe decidere di DEFAULT utilizzare alcuni WebApi, è lì perché, come sopra, è necessario lo WebGrease
. Non sono sicuro di cosa stiano facendo in vNext, probabilmente Newtonsoft.Json
.
Si tratta di prestazioni e flessibilità. Per le prestazioni, vedere http://james.newtonking.com/archive/2012/01/23/json-net-4-0-release-6-serialization-performance.aspx –