2

Web API :

 public int Post(MyModel m){
    return CreateTask(m);
 }

Return value :

Id:"<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">1446</int>"

My question : Why web API returns Id as above.I need it as "1446".How can I get rid of this xml part ?

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • Have you seen this: http://stackoverflow.com/questions/12590801/remove-namespace-in-xml-from-asp-net-web-api This also may be helpful: http://stackoverflow.com/questions/18390709/webapi-serialization-problems-when-consuming-recurly-rest-api-which-returns-xml – David Tansey Sep 30 '14 at 21:09
  • 1
    WebAPI will answer (usually) return xml or JSON to the client. If you need a different type of answer you need to write a handler that does this. But that seems to over complicate things imho. Can't the client just accept JSON or xml? And convert the reply into an integer? – Håkan Fahlstedt Sep 30 '14 at 21:14
  • Just to clarify your question. You are getting xml but want to get Json or you just want a plain string? – Matthijs Wessels Sep 30 '14 at 22:24

3 Answers3

2

WebApi project is configured in the Global.asax; it is there where you will find a class named WebApiConfig. Inside this class you will find the "Media Formatters"; the Media Formatters says whether or not your WebApi is capable of serialize/deserialize JSON System.Net.Http.Formatting.JsonMediaTypeFormatter(), XML or any other format.

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
           //...

            System.Web.Http.GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            config.Formatters.Insert(0, new System.Net.Http.Formatting.JsonMediaTypeFormatter());
            config.Formatters.Insert(0, new System.Net.Http.Formatting.FormUrlEncodedMediaTypeFormatter());


        }
    }

If the JSON formatter is the first item in your list it will be your default serializer/deserializer in order to access any other format the content type of the request should explicitly indicate the desired format if it is supported it will return it and if not it will return it in the default format.

The result of the output you are seeing is entirely responsibility of the deserialization/serialization that the selected media formatter is using.

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
  • 1
    Great.Thanks.Your solution is working.But I would like to ask one question.Even though without using above json formatter it worked for most of the times.Why such a strange behaviour ? – Sampath Sep 30 '14 at 21:27
  • 1
    The only thing that comes to my mind that can make the behavior to change is the content-type of the request. – Dalorzo Sep 30 '14 at 21:28
  • This will cause problems later when another developer comes in and expect the WebAPI to work normally. – frenchie Sep 30 '14 at 21:35
  • @frenchie you are totally wrong about this... As explain in my post the first media formatter is the default the other will work based on content-type. Your understanding of wrong is wrong. – Dalorzo Sep 30 '14 at 21:37
  • You're dealing with modifying the default no? – frenchie Sep 30 '14 at 21:39
  • @frenchie Again, you are wrong... Did you read my post? There is nothing wrong with changing the order of the media formatters or removing the unnecessary ones it is actually better to avoid problems with unsupported formats. – Dalorzo Sep 30 '14 at 21:39
  • Anyway, both our answers work and the OP will chose based on what works best for him. – frenchie Sep 30 '14 at 21:41
0

If you want to return just 1446 you need to return an HttpResponseMessage, like this:

public HttpResponseMessage Post(Event_model event)
{

HttpResponseMessage TheHTTPResponse = new HttpResponseMessage();
TheHTTPResponse.StatusCode = System.Net.HttpStatusCode.OK;
TheHTTPResponse.Content = new StringContent(Event.CreateEvent(event).ToString(), Encoding.UTF8, "text");

return TheHTTPResponse;

}

If you change the configuration globally then it might cause problems when you implement a web service that needs to return some other format. By returning an HttpResponseMessage you can just worry about the particular method you're dealing with.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • you are totally wrong about this... As explain in my post the first media formatter is the default the other will work based on content-type. Your understanding of wrong is wrong – Dalorzo Sep 30 '14 at 21:38
-2

In your request, set the Accept header to application/json:

Accept: application/json
Fordio
  • 3,410
  • 2
  • 14
  • 18