Mike Hofer
Mike Hofer

Reputation: 17002

Accessing versioned routes returns 404 once Swashbuckle is added to a .NET Core Web API application

I created a minimum viable sample to isolate this issue I'm experiencing. I'm sure it's something I'm missing, and not the tools themselves. I'm seeking help figuring out what I'm missing and how to resolve it.

I have a barebones .NET Core 3.1 Web API. It contains 2 controllers (Version 1 and Version 2) that expose the same endpoint and have the same names:

If I do not add Swashbuckle to the application, I can hit these endpoints and see the expected response just fine through a web browser. (The response is HTTP 200 and the response body contains the expected text.)

However, once I add Swashbuckle to the solution, the Swagger page appears and everything looks fine, but I can no longer hit the endpoints through either the Swagger UI or through the browser itself.

Instead, if the version is specified in the route using [Route('api/{version}/foo')], the response is HTTP 404 with the following in the response body:

{
  "error": {
    "code": "UnsupportedApiVersion",
    "message": "The HTTP resource that matches the request URI 'http://localhost:3011/api/v2/foo/status' is not supported.",
    "innerError": null
  }
}

Alternatively, if the version is hardcoded into the route using [Route('api/v2/foo')], the response is HTTP 400, with the following in the response body:

{
  "error": {
    "code": "UnsupportedApiVersion",
    "message": "The HTTP resource that matches the request URI 'https://localhost:44343/api/v2/foo/status' is not supported.",
    "innerError": null
  }
}

I've tried everything I can find in various blog posts, StackOverflow questions, and Microsoft articles to get this working, to no avail, including the following:

So far, no solution has worked.

The solution containing the MVP project can be found in GitHub at this location: https://github.com/DreadLordMikey/NetCoreWebApiMVP

Upvotes: 0

Views: 783

Answers (1)

Rena
Rena

Reputation: 36635

Change the two controller route to:

[Route("api/v{version:apiVersion}/foo")]

And remove options.SubstitutionFormat below:

services.AddVersionedApiExplorer(options =>
{
    options.AssumeDefaultVersionWhenUnspecified = true;
    options.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
    options.GroupNameFormat = "'v'VVV";
    options.SubstituteApiVersionInUrl = true;
    //options.SubstitutionFormat = "'v'VVV";
});

Result:

enter image description here

Upvotes: 1

Related Questions