Skip to content

Using versioning on ASP.NET Core 2.1 overrides default routing #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dodyg opened this issue Sep 4, 2018 · 3 comments
Closed

Using versioning on ASP.NET Core 2.1 overrides default routing #340

dodyg opened this issue Sep 4, 2018 · 3 comments

Comments

@dodyg
Copy link

dodyg commented Sep 4, 2018

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 '/`.

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
  }
}

My full source is here https://github.com/dodyg/practical-aspnetcore/blob/master/projects/mvc/api-versioning/src/Program.cs

@commonsensesoftware
Copy link
Collaborator

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:

services.AddApiVersioning(options => options.AssumeDefaultVersionWhenUnspecified = true);

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.

services.AddApiVersioning(options => options.AssumeDefaultVersionWhenUnspecified = true);

[ApiVersionNeutral]
public abstract class UIController : Controller
{
  protected UIController() { }
}

public class HomeController : UIController
{
}

// OR

[ApiVersionNeutral]
public class AboutController : Controller
{
}

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.

I hope that helps.

@commonsensesoftware
Copy link
Collaborator

Were you able to make one of these options work for you?

@commonsensesoftware
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants