Skip to content

multi-language versioned API #113

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
jsbsantos opened this issue Apr 19, 2017 · 3 comments
Closed

multi-language versioned API #113

jsbsantos opened this issue Apr 19, 2017 · 3 comments
Assignees

Comments

@jsbsantos
Copy link

jsbsantos commented Apr 19, 2017

Hello!

I just found out about this repo and was about to test it out on my current project but I can't seem to get thing to work as I want.

I had these routes defined, which I wanted to change so I could use URL path versioning.

config.Routes.MapHttpRoute(
    name: "DefaultApiLocalized",
    routeTemplate: "{language}-{culture}/api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

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

I expected that changing the routes as follows it would still work:

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

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

After reading a bit on past issues I found out that it's not supposed to work. However, I haven't found a way to make this work.

My question is, can I make these routes work:

/api/v1/my
en/api/v1/my

I have tried using this without success.

[RoutePrefix("{language?}api/v{version:apiVersion}/test")]

Thanks.

@commonsensesoftware
Copy link
Collaborator

Glad you found the project. Welcome!

I'm a little confused. Are you using attribute or convention-based routing? I see parts of both. I strongly recommend using one or the other. You may go cross-eyed otherwise. There are also some limitations in Web API's routing when both are used.

For a convention-based route, this should work:

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

I don't recall ever using a default value in a route prefix, but I believe for an attribute-based route, this should work:

[ApiVersion( "1.0" )]
[RoutePrefix("{language=en}/api/v{version:apiVersion}/test")]
public class TestController : ApiController
{
  [Route]
  public IHttpActionResult Get() => Ok();
}

I also noticed in your route prefix that you were missing the / after the {language} route parameter.

I haven't tried these myself, but I suspect they may work. Give it a whirl and let me know.

@jsbsantos
Copy link
Author

You're right, it came out a bit confusing. What i meant was that I tested both configurations, but I'm not using them both.

Either way, your suggestions didn't work. It may have something to do with my configuration, I don't know, but we've moved the language parameter to the Accept-Language HTTP header.

Thanks.

@commonsensesoftware
Copy link
Collaborator

Sounds good.

I think you made the right decision. The Accept-Language header will give you a lot more flexibility in requests because it supports multiple values with quality. You'll have the option to support language fallback without any changes to the API. :)

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