Hi,
I would like to configure Swagger (Swashbuckle vr 5.0.0) in my project (.NET Core 3.1) with OData (vr 7.3). Currently I get an exception:
{"message":"No media types found in 'Microsoft.AspNet.OData.Formatter.ODataOutputFormatter.SupportedMediaTypes'. Add at least one media type to the list of supported media types. "}
Microsoft.AspNetCore.OData 7.3.0
Swashbuckle.AspNetCore 5.0.0
.NET Core 3.1
1) Create project with .NET Core 3.1
2) Configure Swashbuckle (my configuration it's here https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1442)
3) Create ODATA endpoint & standard endpoint
4) Run application
You will see at least standard endpoint.
You will see Swagger page with information 'Failed to load API Definition'
In my opinion API/Swagger shouldn't generate OData endpoints because they are marked as ignored ([ApiExplorerSettings(IgnoreApi = true)])
We managed to get this working by adding this:
services.AddMvcCore(options =>
{
foreach (var outputFormatter in options.OutputFormatters.OfType
{
outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
foreach (var inputFormatter in options.InputFormatters.OfType
{
inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
});
Ok, the solution is:
services.AddMvcCore(options =>
{
foreach (var outputFormatter in options.OutputFormatters.OfType<OutputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
{
outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
foreach (var inputFormatter in options.InputFormatters.OfType<InputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
{
inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
});
Thanks Mhirji :)
I have created a service extension for the same.
Please check following link.
https://github.com/KishorNaik/Sol_OData_Swagger_Support
Works to me. Important note: formatters must be fixed AFTER odata is registered. This is why my original code AddMvcCore(...).AddOData() didn't work. Be careful.
services.AddMvcCore(options =>
{
foreach (var outputFormatter in options.OutputFormatters.OfType().Where(x => x.SupportedMediaTypes.Count == 0))
{
outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}foreach (var inputFormatter in options.InputFormatters.OfType
().Where(x => x.SupportedMediaTypes.Count == 0))
{
inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
});
It works
Most helpful comment
Ok, the solution is:
Thanks Mhirji :)