0

I am working on an ASP.NET Core 5 API project and I am using Microsoft.AspNetCore.OData v8.0.0-preview2.

If I use the out-of-box ASP Net Web application template, before I add OData, Swagger works just fine.

However, as soon as I add the following code;

services.AddOData(
    option => option.AddModel("v1", GetV1EdmModel())
    .Select()
    .Expand()
    .Filter()
    .OrderBy()
    .Count());

... to my Startup.cs --> ConfigureServices method and then try to navigate to the ../swagger endpoint the swagger page loads but displays the following error;

Failed to load API definition

Fetch error
unidentified /swapper/v1/swagger.json

Is swagger not yet supported in the Microsoft.AspNetCore.OData v8.0.0-preview2 release? Or am I missing something.

I have read through Sam Xu's blog posts; ASP.NET Core OData 8.0 Preview for .NET 5 and Routing in ASP.NET Core OData 8.0 Preview

But there was no mention of this issue.

Does anyone have a solution or a work around?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
whiskytangofoxtrot
  • 927
  • 1
  • 13
  • 41
  • Is the URL starting with /swapper correct? Do you see any errors in the output window (if IIS Express)/ASP.NET Core console (if Kestrel)? What about in the browser's developer console? We need more info to help you. – Camilo Terevinto Dec 01 '20 at 19:22
  • Swagger is not compatible with oDatar as far as I know. There's a better, fuller, answer here. https://stackoverflow.com/questions/32858371/what-is-swagger-and-does-it-relate-to-odata – dogyear Dec 01 '20 at 19:36
  • @CamiloTerevinto yes the URL before /swagger is correct. I can use the same URL but point to one of my controller endpoints and that part works. Just not the swagger. As I noted, if I comment out the services.addOData() in my startup.cs, swagger then works so the issue is with the new v8.0.0-preview2 of Microsoft.AspNetCore.Odata – whiskytangofoxtrot Dec 03 '20 at 15:23
  • @dogyear you may have a point. I have swagger working with Microsoft.AspNetCore.OData v7.x on another project but had forgotten about it. After looking at that code, I can see I am using the swashbuckle nuget for OData. I had assumed that this was an issue with the new Microsoft.AspNetCore.Odata v8.0.0-preview2. I will test with the swashbuckle OData nuget and post back here if that resolves the issue. – whiskytangofoxtrot Dec 03 '20 at 15:44

1 Answers1

3

You can find the error message in the response:

System.InvalidOperationException: No media types found in 'Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter.SupportedMediaTypes'. Add at least one media type to the list of supported media types.

Try add below code after services.AddOData(...).

services.AddMvcCore(options =>
{
    foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
    {
        outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
    }
    foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
    {
        inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
    }
});

And the same issue on github.

mj1313
  • 7,930
  • 2
  • 12
  • 32