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 ?

3
  • Have you seen this: stackoverflow.com/questions/12590801/… This also may be helpful: stackoverflow.com/questions/18390709/… Commented Sep 30, 2014 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? Commented Sep 30, 2014 at 21:14
  • Just to clarify your question. You are getting xml but want to get Json or you just want a plain string? Commented Sep 30, 2014 at 22:24

3 Answers 3

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.

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

7 Comments

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 ?
The only thing that comes to my mind that can make the behavior to change is the content-type of the request.
This will cause problems later when another developer comes in and expect the WebAPI to work normally.
@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.
You're dealing with modifying the default no?
|
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.

1 Comment

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
-2

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

Accept: application/json

1 Comment

He doesn't mention that, he seems to be trying to get rid of the xml part.

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.