From b8645eeb314791acf10eebb6578037825397e65d Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Fri, 31 Oct 2014 15:01:51 -0700 Subject: [PATCH] Follow up to JsonResult changes Changing the WebAPI shim to use the new and improved JsonResult. --- .../ActionResults/JsonResult.cs | 2 +- .../ApiController.cs | 35 +++++++------------ .../ApiControllerTest.cs | 16 ++++----- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ActionResults/JsonResult.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ActionResults/JsonResult.cs index 79a025ed2c..e3c4fc7fb8 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ActionResults/JsonResult.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ActionResults/JsonResult.cs @@ -3,6 +3,6 @@ namespace System.Web.Http { - [Obsolete("This class is obsolete. Use Microsoft.AspNet.Mvc.ObjectResult as a replacement.", error: true)] + [Obsolete("This class is obsolete. Use Microsoft.AspNet.Mvc.JsonResult as a replacement.", error: true)] public class JsonResult { } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs index 9c4dd55370..da9598f990 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs @@ -220,52 +220,45 @@ namespace System.Web.Http } /// - /// Creates an (200 OK) with the specified value. + /// Creates an (200 OK) with the specified value. /// /// The type of content in the entity body. /// The content value to serialize in the entity body. - /// A with the specified value. + /// A with the specified value. [NonAction] - public virtual ObjectResult Json([NotNull] T content) + public virtual JsonResult Json([NotNull] T content) { - var result = new ObjectResult(content); - result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("application/json")); - result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("text/json")); - return result; + return new JsonResult(content); } /// - /// Creates an (200 OK) with the specified values. + /// Creates an (200 OK) with the specified values. /// /// The type of content in the entity body. /// The content value to serialize in the entity body. /// The serializer settings. - /// A with the specified values. + /// A with the specified values. [NonAction] - public virtual ObjectResult Json([NotNull] T content, [NotNull] JsonSerializerSettings serializerSettings) + public virtual JsonResult Json([NotNull] T content, [NotNull] JsonSerializerSettings serializerSettings) { var formatter = new JsonOutputFormatter() { SerializerSettings = serializerSettings, }; - var result = new ObjectResult(content); - result.Formatters.Add(formatter); - result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("application/json")); - result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("text/json")); - return result; + return new JsonResult(content, formatter); } /// - /// Creates an (200 OK) with the specified values. + /// Creates an (200 OK) with the specified values. /// /// The type of content in the entity body. /// The content value to serialize in the entity body. /// The serializer settings. /// The content encoding. - /// A with the specified values. + /// A with the specified values. [NonAction] - public virtual ObjectResult Json( + public virtual JsonResult Json( [NotNull] T content, [NotNull] JsonSerializerSettings serializerSettings, [NotNull] Encoding encoding) @@ -278,11 +271,7 @@ namespace System.Web.Http formatter.SupportedEncodings.Clear(); formatter.SupportedEncodings.Add(encoding); - var result = new ObjectResult(content); - result.Formatters.Add(formatter); - result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("application/json")); - result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("text/json")); - return result; + return new JsonResult(content, formatter); } /// diff --git a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ApiControllerTest.cs b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ApiControllerTest.cs index 4b15bdddc3..996baeffb3 100644 --- a/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ApiControllerTest.cs +++ b/test/Microsoft.AspNet.Mvc.WebApiCompatShimTest/ApiControllerTest.cs @@ -243,8 +243,8 @@ namespace System.Web.Http var result = controller.Json(product); // Assert - var objectResult = Assert.IsType(result); - Assert.Same(product, objectResult.Value); + var jsonResult = Assert.IsType(result); + Assert.Same(product, jsonResult.Value); } [Fact] @@ -259,10 +259,10 @@ namespace System.Web.Http var result = controller.Json(product, settings); // Assert - var objectResult = Assert.IsType(result); - Assert.Same(product, objectResult.Value); + var jsonResult = Assert.IsType(result); + Assert.Same(product, jsonResult.Value); - var formatter = Assert.IsType(Assert.Single(objectResult.Formatters)); + var formatter = Assert.IsType(jsonResult.Formatter); Assert.Same(settings, formatter.SerializerSettings); } @@ -278,10 +278,10 @@ namespace System.Web.Http var result = controller.Json(product, settings, Encoding.UTF8); // Assert - var objectResult = Assert.IsType(result); - Assert.Same(product, objectResult.Value); + var jsonResult = Assert.IsType(result); + Assert.Same(product, jsonResult.Value); - var formatter = Assert.IsType(Assert.Single(objectResult.Formatters)); + var formatter = Assert.IsType(jsonResult.Formatter); Assert.Same(settings, formatter.SerializerSettings); Assert.Same(Encoding.UTF8, Assert.Single(formatter.SupportedEncodings)); }