XML/JSON transformation in ASP.NET Web API RRS feed

  • Question

  • User1520717856 posted

    How to achieve unidirectional and bidirectional XML/JSON transformation in ASP.NET Web API?

    Saturday, August 15, 2015 3:38 PM


  • User-782957977 posted

    Web Api will automatically do XML/JSON transformation based on Accept type/Content Type in the request. This is done using Content negotiation feature in Web Api http://www.asp.net/web-api/overview/formats-and-model-binding/content-negotiation

    If Web Api is called from .Net, you can specify accept type like this. Please give accept type as application/xml for Xml format.

    using (var client = new HttpClient())
                     client.BaseAddress = new Uri("http://localhost:58745");
                     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                     var response = client.GetAsync("api/values").Result;
                     if (response.IsSuccessStatusCode)
                         string responseString = response.Content.ReadAsStringAsync().Result;

    If Web Api called from  JQuery, then specify conttype like this

                        type: "get",
                        url: "http://localhost:58745/Api/Values",
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        data: {},
                        success: function (result) {
                        failure: function () {

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 15, 2015 9:32 PM