Jeśli projekt był tylko projekt MVC bez WebAPI, następnie Newtonsoft.Json
nie został dodany do powrotu JsonResults
jako JsonResult
zwróconej przez MVC wykorzystuje JavaScriptSerializer
poniżej:
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));
}
}
W tym przypadku została dodana, ponieważ WebGrease
jest zależny od niego. Usługi łączenia i oczyszczania oferowane przez MVC pod numerem System.Web.Optimization
są zależne od WebGrease
.
W związku z tym domyślna aplikacja MVC bez WebApi będzie miała zainstalowane Newtonsoft.Json
dla usług związanych z pakowaniem i minimalizacją, a nie WebApi.
Aby być jasne JsonResult
zwrócony przez WebAPI w System.Web.Http
używa Newtonsoft.Json
za to serializacji jak poniżej:
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
Ale Newtonsoft.Json
nie wliczono w nie WebAPI, domyślnie projekt MVC tylko w przypadku, możesz zdecydować się na użyj WebApi, jest tam, ponieważ, jak wyżej, potrzebuje tego WebGrease
. Nie wiem, co robią w vNext, prawdopodobnie Newtonsoft.Json
.
Chodzi o wydajność i elastyczność. Aby uzyskać więcej informacji, zobacz http://james.newtonking.com/archive/2012/01/23/json-net-4-0-release-6-serialization-performance.aspx –