0

I have found a similar question that relates to standard asp.net controllers, but i have tried it and it doesn't seem to be working with the apicontroller.

Restrict route to controller namespace in ASP.NET Core

I want to allow for an API to be deprecated in the future without needing to change the url. To solve this i will put a version prefix at the start of the route. This way i can add some modifications to an endpoint without breaking any integrations with this API.

    config.Routes.MapHttpRoute(
        name: "V1 API",
        routeTemplate: "v1/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }

    );
    config.Routes.MapHttpRoute(
        name: "V2 API",
        routeTemplate: "v2/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }

    );

I want to keep the same controller names and in most cases everything will be the exact same, the response and request body will be the only things that change. I have tried to add a subdirectory in the controllers folder in order to add v1 and v2 to the namespace of the controller, but i get an error about there being 2 controllers having the same name.

How can i modify the routes so that it will point to the v1 namespace for the controller instead of just searching for any class within the controller. I dont want to have to add the version to the controller name as i want to make the revision updates as seamless as possible.

2 Answers 2

0

I think you could solve this issue by adding the route prefix to each controller.

Sign up to request clarification or add additional context in comments.

Comments

0

Can you use a URL query parameter?

https://localhost:8000/{controller}/{action}/{id}?version=?

You could then use control flow within the controller to determine what logic to use, based on the version.

2 Comments

How could I use this to route the request to the correct controller?
If you want to use separate controllers for each version, the solution @TarekAboELkheir posted would work. The only issue I find is that v2{ControllerName} looks a bit clunky.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.