1

I am working on a web API with ASP.NET MVC (.NET 4.5.2) (I'm quite new to ASP) and I would like the change to response format from my controller to be JSON instead of XML.

I tried several things like using the ActionResult return type and returning something like new Json() but this function is not recognized and Visual Studio asks me to create the function.

I'm not sure I'm giving you enough info to help me, so please ask me for more if necessary :)

Thanks!

11
  • maybe this helps: stackoverflow.com/questions/9847564/… Commented Feb 27, 2015 at 8:26
  • Web API will chose which format to return the data based on the "Accept" header - so if you call your API from a browser it will return XML as that is included in the browser's Accept list, if you call it from an AJAX request it will return json. If you want to see the json in a browser, capture a browser request to your API with fiddler and alter the ACCEPT header to be "application/json" Commented Feb 27, 2015 at 8:27
  • @Jeahel can you provide code how you try? Commented Feb 27, 2015 at 8:29
  • @Jehael its not an object you need to create... not new Json()... its type is JsonResult and this type is returned by calling the Json() method... msdn.microsoft.com/en-us/library/dd504936%28v=vs.118%29.aspx Commented Feb 27, 2015 at 8:44
  • @MemetOlsen this helps; thanks! But then I have two questions: I configured the api to return text/html and in that case i'm not sure how to escape quotes. If I write return "{\"decision\":\"enable\"}"; in my controller, I get "{\"decision\":\"enable\"}" in my browser (so not valid JSON). On the other hand, if I configure with application/json, I still get XML but this time, the string in the XML is fine. Can't I use application/json in the configuration ? Commented Feb 27, 2015 at 8:47

2 Answers 2

3

All you need to do is this:

 [HttpGet]
 public object Test(string testparameters)
 {
    return new {decision = "enable"};
 }
Sign up to request clarification or add additional context in comments.

10 Comments

Yeah, this is the answer. You can return your models or anonymous types this way.
JsonRequestBehavior.AllowGet not need if request not GET :-)
Ok so with this method I get {"ContentEncoding":null,"ContentType":null,"Data":{"decision":"enable"},"JsonRequestBehavior":0,"MaxJsonLength":null,"RecursionLimit":null} which is a valid Json string (of course).The problem is that the backend I send my response to waits for pure data, so only the Data variable of my current Json response. any way to do that ?
Nope I don't :-/ it's juste how the framework works, it needs this response and nothing else
@FlorianSchmidinger This is the response I get "{\"decision\":\"enable\"}". I think the escape slashes make the Json invalid, right?
|
2

If you are making use of an ApiController, the client (probably the browser that makes the HTTP request) should specify which type it is expecting.

When the client sends a request message, it can include an Accept header. The Accept header tells the server which media type(s) the client wants from the server. For example:

Accept: text/html,application/xhtml+xml,application/xml

This header tells the server that the client wants either HTML, XHTML, or XML.

The media type determines how Web API serializes and deserializes the HTTP message body. Web API has built-in support for XML, JSON, BSON, and form-urlencoded data, and you can support additional media types by writing a media formatter.

See Media Formatters in ASP.NET Web API 2.

In your case, your request should contain Accept: application/json

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.