SDammann.WebApi.Versioning icon indicating copy to clipboard operation
SDammann.WebApi.Versioning copied to clipboard

Is Attribute Routing Supported

Open KenBrannigan opened this issue 8 years ago • 2 comments

@Sebazzz first thank you for creating this package it is exactly what I was looking for.

Currently I am using the following route:

config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/v{version}/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );

My controllers are in the following directory structure:

Controllers -Version1 --AccountController.cs -Version2 --AccountController.cs (Inherits from Version1 controller)

Everything works perfectly but I wanted to see if your package supports attribute routing and if it does will it work when the Version2 controller is inheriting from the Version1 controller? I did attempt this but I got some key error which I have seen posted in the issues list before.

I am currently using version 2.8.0 of the package.

Thank you, Ken

KenBrannigan avatar Feb 15 '17 03:02 KenBrannigan

If you're using attribute routing I suggest you inherit both controllers from a base class, and putting a routeprefix in the version implementation. Otherwise, check out the newer beta version.

Sebazzz avatar Feb 15 '17 15:02 Sebazzz

@Sebazzz I upgraded to the 3.0 beta as you stated.

Here is my configuration code which uses Autofac for by dependency injector:

`// Create our autofac dependency container. ContainerBuilder builder = new ContainerBuilder();

        // Add controller to our dependency container.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Add items to our dependency container needed for our api versioning.
        builder.Register(c => new DefaultControllerIdentificationDetector(config));
        builder.Register(c => new DefaultRequestControllerIdentificationDetector(config));
        
        // Build our dependency container and set as our dependency resolver.
        IContainer container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        
        // Setup our routes.
        config.MapHttpAttributeRoutes();

        // Set our url based api versioning.
        ApiVersioning.Configure(config)
            .ConfigureRequestVersionDetector<DefaultRouteKeyVersionDetector>();`

Again my controller configuration is as follows:

Controllers -Version1 --AccountController.cs -Version2 --AccountController.cs (Inherits from Version1 controller)

An example of a route attribute I placed on the version 1 controller:

[Route("v{version}/account/login")]

When I request this route using Postman I get a 404 error.

Can you confirm this setup should be supported using the beta version and if so what configuration changes do you recommend to pull it off?

Also what lifetime scope should be applied to the two items added to the dependency injector:

DefaultControllerIdentificationDetector DefaultRequestControllerIdentificationDetector

Thank you in advance for any help you can provide.

Take care, Ken

KenBrannigan avatar Feb 16 '17 03:02 KenBrannigan