Customizing Json from helper

Add Json to Helper
Guarantee SerializerSettings
This commit is contained in:
Yishai Galatzer 2014-02-03 20:12:05 -08:00
parent 4b4034788c
commit 2a3f891878
3 changed files with 12 additions and 3 deletions

View File

@ -42,12 +42,20 @@ namespace MvcSample
public IActionResult UserJson()
{
return new JsonResult(new User() { Name = "User Name", Address = "Home Address" })
return new JsonResult(_user)
{
Encoding = Encoding.UTF8
};
}
public IActionResult HelperUserJson()
{
JsonResult jsonResult = (JsonResult)Result.Json(_user);
jsonResult.Indent = false;
return jsonResult;
}
public User User()
{
return _user;

View File

@ -32,7 +32,7 @@ namespace Microsoft.AspNet.Mvc
public IActionResult Json(object value)
{
throw new NotImplementedException();
return new JsonResult(value);
}
public IActionResult View(string view, ViewData viewData)

View File

@ -24,6 +24,7 @@ namespace Microsoft.AspNet.Mvc
Encoding = Encoding.UTF8;
_returnValue = returnValue;
_jsonSerializerSettings = CreateSerializerSettings();
}
public JsonSerializerSettings SerializerSettings
@ -84,7 +85,7 @@ namespace Microsoft.AspNet.Mvc
JsonWriter jsonWriter = new JsonTextWriter(new StreamWriter(writeStream, effectiveEncoding));
if (Indent)
{
jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
jsonWriter.Formatting = Formatting.Indented;
}
return jsonWriter;