1

I want to be able to identify a resource by different id types. for example:

GET http://example.com/customers/internalId=34 to go to

public Customer GetByInternalId(int internalId){...}

and GET http://example.com/customers/externalId='JohnDoe' to go to

public Customer GetByExternalId(string externalId){...}

I know I can do this by having some parsing logic in a generic controller method but I don't want to do that. How do I achieve this using the routing feature of asp.net webapi if that is possible.

2 Answers 2

1

I would suggest that you try and avoid doing what you are suggesting. Creating two distinct URIs for the same resource will make it harder to use caching. Instead I would suggest using one URL to redirect to the other.

e.g.

> GET /customers/34
< 200 OK


> GET /Customers?name=JohnDoe
< 303 See Other
< Location: http://example.com/customers/34
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Darren. I've seen that answer before. I am trying to do what linked in does here:developer.linkedin.com/documents/profile-api and would like to know if such routing is possible with WebApi.
@darthjit For sure it is possible with WebAPI because you can replace the routing component and implement it yourself. Whether existing routing mechanism can do it, I don't know because I would never attempt to do such a thing. Using the tilde to return the "current profile" is an especially dumb thing for Linked in to do.
0

Your methods do not make much of a sense, why would you return void from a method that begins with Get....?

Also, these routes:

http://example.com/customers/internalId=34
http://example.com/customers/externalId='JohnDoe

Are invalid from MVC/Web API perspective. This is how they should look like:

http://example.com/customers?internalId=34
http://example.com/customers?externalId=John

Default Web API routing should differentiate between the two and route it to different actions.

EDIT:

Create action with the following template:

[HttpGet]
public string InternalId(int id)
{
    return id.ToString();
}

Define route for Web Api:

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

This allows you to write:

http://localhost:7027/values/internalId=12

Try it...

Then you can just add another method:

[HttpGet]
public string ExternalId(string id)
{
    return id;
}

And this:

http://localhost:7027/values/externalId=bob

Will work as well.

Clearly name of my controller is ValuesController as I've just tested this with default Web Api template.

2 Comments

Thanks for pointing out the error in methods. While I realize that query strings will work, that is not what I am looking for. I would like to do what linked in has done here: developer.linkedin.com/documents/profile-api , api.linkedin.com/v1/people/id=abcdefg , api.linkedin.com/v1/people/url=<public-profile-url>
I've updated my post with how you can do what you're trying to do. I believe you'll get the idea if you pay close attention to the route I've defined.

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.