0

I'm using swagger to test my ASP.NET Core Web API project controller's method. Swagger sees all my end points and lets me call them without issue. Swagger returns the number of records expected either one or many and all the field names are correct, but all are missing the data.

All my controllers are returning JObjects or in case of GetAll() - `System.Collections.Generic.IEnumerable<Newtonsoft.Json.Linq.JObject>.

The Agency table contains two records. Here is what Swagger displays:

[
  {
    "agency_name": [],
    "agency_url": [],
    "agency_timezone": [],
    "agency_lang": [],
    "agency_phone": [],
    "agencyID": [],
    "Divisions": []
  },
  {
    "agency_name": [],
    "agency_url": [],
    "agency_timezone": [],
    "agency_lang": [],
    "agency_phone": [],
    "agencyID": [],
    "Divisions": []
  }
]

It doesn't matter if I return 1 record or many.

I have set a break point at the last "return" line of the controller method, and it shows that data is being returned. Yet it is not showing in the Swagger UI.

I feel like I have left out an obvious step. Perhaps in the Program class?

Any guidance will be appreciated.

Erick.

1 Answer 1

0

It seems Swagger out of the box doesn't like JObjects. I found a similar question back here Why asp.net core sending empty object as response?

The solution was provided by farman-ameer and others.

I'm not sure if it applies to all .NET versions. But for .NET 6.0 it worked. You need to reference Nuget package Microsoft.AspNetCore.Mvc.NewtonsoftJson in your project. I didn't have to add a using reference to this assembly in the program class. Then in your "ConfigureServices" method of your Program class add/modify this line "services.AddControllers().AddNewtonsoftJson();"

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

Comments

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.