If you are trying to use Swashbuckle with an ASP.NET Core project that uses OData, you are going to get an error on the swagger endpoint. The error will be something like this.
InvalidOperationException: No media types found in ‘Microsoft.AspNet.OData.Formatter.ODataOutputFormatter.SupportedMediaTypes’. Add at least one media type to the list of supported media types.
Until one of these guys between them fix this, here’s the hackish fix. It won’t enable Swagger for the OData controllers, but it will stop Swagger from breaking for the other controllers.
services.AddMvc(op => { foreach (var formatter in op.OutputFormatters .OfType<ODataOutputFormatter>() .Where(it => !it.SupportedMediaTypes.Any())) { formatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue("application/prs.mock-odata")); } foreach (var formatter in op.InputFormatters .OfType<ODataInputFormatter>() .Where(it => !it.SupportedMediaTypes.Any())) { formatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue("application/prs.mock-odata")); } });
I find I still get this error, even with the work around in place.
This was were useful it solve my problem. Thank you
using dotnet core 3.1 and added the above code to services.AddControllers and note to services.AddMvc but I still get the error.
Same for me