1

I am creating Controller to return an object in JSON format. I want to use Newtonsoft.Json library. I created two methods:

[HttpGet]
[ActionName("RetrieveTestClassJson")]
public ActionResult RetrieveTestClassJson(int id)
{
    TestThing testThing = new TestThing() { Name = "LPL.22.334", Type = TypeTest.Component };
    JsonNetResult jsonNetResult = new JsonNetResult();
    jsonNetResult.Formatting = Formatting.Indented;
    jsonNetResult.ContentType = "application/json";
    jsonNetResult.ContentEncoding = Encoding.Unicode;
    jsonNetResult.Data = testThing;
    return jsonNetResult;
}

[HttpGet]
[ActionName("RetrieveTestClassCase2")]
public TestThing RetrieveTestClassCase2(int id)
{
    TestThing testThing = new TestThing() { Name = "LPL.22.334", Type = TypeTest.Component };
    return testThing;
}

when I call RetrieveTestClassJson from ajax or browser url I get:

{"ContentEncoding":{"isThrowException":false,"bigEndian":false,"byteOrderMark":true,"m_codePage":1200,"dataItem":null,"encoderFallback":{"strDefault":"�","bIsMicrosoftBestFitFallback":false},"decoderFallback":{"strDefault":"�","bIsMicrosoftBestFitFallback":false},"m_isReadOnly":true},"ContentType":"application/json","Data":{"Name":"LPL.22.334"},"SerializerSettings":{"ReferenceLoopHandling":0,"MissingMemberHandling":0,"ObjectCreationHandling":0,"NullValueHandling":0,"DefaultValueHandling":0,"Converters":[{"CamelCaseText":true,"CanRead":true,"CanWrite":true}],"PreserveReferencesHandling":0,"TypeNameHandling":0,"TypeNameAssemblyFormat":0,"ConstructorHandling":0,"ContractResolver":null,"ReferenceResolver":null,"TraceWriter":null,"Binder":null,"Error":null,"Context":{"m_additionalContext":null,"m_state":0},"DateFormatString":"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK","MaxDepth":null,"Formatting":0,"DateFormatHandling":0,"DateTimeZoneHandling":3,"DateParseHandling":1,"FloatFormatHandling":0,"FloatParseHandling":0,"StringEscapeHandling":0,"Culture":"(Default)","CheckAdditionalContent":false},"Formatting":1}

When I call RetrieveTestClassCase2 I get normal JSON format. I removed xml handler for testing purposes for now:

var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

I am puzzled of what is going on.

2 Answers 2

6

WebApi already has the Json serializer in the pipeline, http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization, so I think in your method where you are explicitly serializing, the result ends up serialized twice. The second method is what you want unless you remove the json formatter as well.

Sign up to request clarification or add additional context in comments.

2 Comments

How can I customize/override serialization process, let's if i want to add characters to serialized string in order to prevent JSON Vulnerability (haacked.com/archive/2008/11/20/…)?
Some examples of customizing the serialization process.. tostring.it/2012/07/18/customize-json-result-in-web-api
4

The reason it's return JSON when you don't do anything, is because MVC has its own build in JSON serialzer. Since this is a

[System.Web.Http.HttpGet]

not

[System.Web.Mvc.HttpGet]

it treats what your sending as data and not as markup. That's why it's returning your object as JSON. It knows that your sending just an object, so it serializes it for you. If you wanted the same functionality in a [System.Web.Mvc.HttpGet], you would return a JsonResult instead of an ActionResult.

var result = new JsonResult();
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
result.Data = testThing;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.