0

I guess that will be a duplicated question because it's something quite basic, but after some search I was not able to find the answer.

I have a .net462 application. I have the following enum:

 public enum Test
 {
     Foo,
     Bar,
 }

and the following endpoint in one controller:

 [HttpGet]
 [Route("myTest")]
 public IEnumerable<Test> GetMyFooTest()
 {
     return new List<Test> { Test.Bar, Test.Foo };
 }

Now, if I use chrome to retrieve data from the endpoint, I get: Browser screenshot

Here the Enums appear in its string form.

However, when I call this endpoint from the front end (JS, jQuery), or if I call it using Postman, I get: Postman screenshot

No matter if I visualise it with JSON, XML or any option: options

Here I get the numerical values of the Enums.

Why this difference? Is there a way to get the string values in the front end? (of course I have the option to change the controller to return a IEnumerable<string> and use a ToString() on the c# side, but that would be quite ugly...).

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
xavier
  • 1,860
  • 4
  • 18
  • 46
  • 1
    do those hidden headers ask for a specific format? – Isparia Jan 14 '22 at 09:51
  • I ask why I get two apparently different answers. In Postman there is also the XML view and I don't get the same results as the XML in the browser. I'd like to see the strings in Postman the same way I see them from the browser. – xavier Jan 14 '22 at 09:52
  • 3
    Send the appropriate `Accept` header then. This seems to be nothing to do with how the enum is represented. – ProgrammingLlama Jan 14 '22 at 09:52
  • 1
    Check the header in both browser and postman to find the difference? – ikhvjs Jan 14 '22 at 09:52
  • Ok, in postman I have Content-Type: application/json. That's it. I knew it was goint to be something basic... Thank you all! – xavier Jan 14 '22 at 09:54
  • 1
    If you want to enforce XML always (remove JSON support), you can see [this question](https://stackoverflow.com/questions/10250098/disable-json-support-in-asp-net-mvc-web-api). Having both allows the browser/client to request the response format (e.g. `Accept: application/xml` or `Accept: application/json`). – ProgrammingLlama Jan 14 '22 at 09:55
  • 1
    I marked the question as a duplicate but I didn't delete it in case it can help someone who don't use the right search words as I did. However, I found another duplicate question that has even better answers: https://stackoverflow.com/q/2441290/11241615 – xavier Jan 14 '22 at 11:04

0 Answers0