0

I've developed an ASP.NET web API which is actually retrieving data from a Fake API But When I'm reading data and from my API controller trying to call that Data Service Function then always my browser showing data in xml format but I want to show it in JSON format.

Here is my code:

Data Service Function:

    public List<UserModel> GetData()
    {
        var list = new List<UserModel>();
        var url = "http://jsonplaceholder.typicode.com/posts";
        String json = string.Empty;
        try
        {
            HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
            webRequest.Method = WebRequestMethods.Http.Get;
            webRequest.ContentType = "application/json; charset=utf-8";
            using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream stream = response.GetResponseStream();
                    StreamReader re = new StreamReader(stream);
                    json = re.ReadToEnd();
                    json = "{\"Users\":" + json + "}";

                    JsonSerializer serializer = new JsonSerializer();
                    wrapper w = JsonConvert.DeserializeObject<wrapper>(json);
                    list = w.UserDetails;
                }
            }
        }
        catch (Exception ex)
        {
            throw (ex);
            //  ShowError(ex);
            // if the server returns a 500 error than the webRequest.GetResponse() method
            // throws an exception and all I get is "The remote server returned an error: (500)."
        }
        return list;
    }

Controller:

    [HttpGet]
    public IEnumerable<UserModel> GetAllUsers()
    {

        try
        {
            DataService ds = new DataService();

            return ds.GetData();
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }

My output coming as this into the browser:

My output coming as this

Please help on the above. Thanks on advance.

barsan
  • 2,431
  • 16
  • 45
  • 62
  • ASP.NET will return it in the format you request, defaulting to XML. Specify that you want json in your Accept header. – ProgrammingLlama Apr 01 '17 at 09:09
  • P.S. I searched for your exact question on Google and found a duplicate. Did you even try? – ProgrammingLlama Apr 01 '17 at 09:09
  • 1
    Possible duplicate of [How do I get ASP.NET Web API to return JSON instead of XML using Chrome?](http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome) – ProgrammingLlama Apr 01 '17 at 09:10
  • BTW try {} catch (Exception ex) { throw(ex); } is useless and will only put noise on the code. – Sir Rufo Apr 01 '17 at 09:26
  • @SirRufo I did not copy paste any code without understanding. May be my way of understanding was different. My understanding was accepting the JSON type should help because previously I have developed another API and that one is giving JSON output without any issue but this new one is giving issue. – barsan Apr 01 '17 at 09:28
  • @barsan There is nothing inside your code where you explicit accept JSON. If the response contains JSON than it is just a lucky shot but nothing you request. Set the Accept property in your request to application/json. The ContentType property is only needed if you Put/Post some content – Sir Rufo Apr 01 '17 at 09:33
  • @SirRufo Thanks for your explanation. Now I've understood my problem. – barsan Apr 01 '17 at 09:36

0 Answers0