You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using API Versioning apparently breaks the default routing. I am not sure whether this is by design or now. With below' code, HomeController is not longer responding to '/`.
public class Startup
{
public Startup(IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration)
{
//These are three services available at constructor
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddApiVersioning();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration)
{
app.UseMvcWithDefaultRoute();
}
}
and returns
// 20180904113308
// http://localhost:5000/
{
"error": {
"code": "ApiVersionUnspecified",
"message": "An API version is required, but was not specified.",
"innerError": null
}
}
This behavior is by design. API versioning doesn't change routing. It supports disambiguating otherwise duplicate routes by API version and imposes certain rules. One of those rules is that all controllers and routes have an API version. In ASP.NET Core there hasn't been a clear was to disambiguate between UI controllers and API controller until recently (but it's not intrinsically used by API versioning -yet). This means that UI controllers have the same constraints as API controllers, even though that's not really what you want.
There are several choices to address you scenario.
Option 1
You can use MapWhen with a route prefix to only apply API versioning to specific routes (ex: ~/api/*).
Option 2
You can allow an API version to be omitted by enabling the setting:
This will treat any unannotated controller, such as UI controller, as having the API version defined by options.DefaultApiVersion. In the case of UI controllers, the actual value is inconsequential.
Option 3
As a variation of Option 2, you can allow no API version and also have your UI controllers accept any API version, including none at all.
In the future, there will better support for the new API Conventions that were introduced (in 2.1 I think). It's also worth noting that this issue does not exist with Razor Pages because they are not actually controllers and, thus, do not have any conventions applied.
As this question has been answered without a response, I am assuming you were able to get things working. If you continue to have issues, feel free to reopen this issue. Thanks.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
Using API Versioning apparently breaks the default routing. I am not sure whether this is by design or now. With below' code, HomeController is not longer responding to '/`.
and returns
My full source is here https://github.com/dodyg/practical-aspnetcore/blob/master/projects/mvc/api-versioning/src/Program.cs
The text was updated successfully, but these errors were encountered: