10

I've been playing with the new Web API 2 (which looks very promising btw) but I'm having a bit of a headache to get some routes working. All works fine when I have GetAllUsers / GetUser(int id), but then when I add GetUserByName(string name) and/or GetUserByUsername(string username) things start to be creepy. I know that the int will be the first one and that I can re-order the routes but let's imagine the following scenario:

A user can have a valid username=1234 or name=1234 (I know it's unlikely but we need to prevent any possible situation) and we might have a valid 1234 ID in the database and all the routes will be mixed up.

Maybe this is something that we will need to work with on the new WebAPI 2 so I thought I could come with an "workaround" passing filters as querystrings to target different action in the same controller, such as api/users/?username=1234 (GetUserByUsername) or api/users/?name=1234 (GetUserByName)

But I cannot make querystrings to come through ... actually any querystring option above is getting caught by the GetAllUsers.

Does anyone have any suggestion/fix for that scenario?

Thanks a lot

1 Answer 1

13

You need to define the method access name like

[HttpGet("User")]
public async Task<UserViewModel> GetByName(string name)
[HttpGet("User")]
public async Task<UserViewModel> GetByUserName(string name)

//You can access like 
//- api/Users/User?name=someneme
//- api/Users/User?username=someneme

OR

[HttpGet("User")]
public async Task<UserViewModel> GetByAnyName(string name="", string username="")
//- api/Users/User?name=someneme
//- api/Users/User?username=someneme
//- api/Users/User?username=someneme&name=someone

UPDATED Above both will work nicely with other configurations of route prefix.

OR

[HttpGet("")]
public async Task<UserViewModel> GetAll()
[HttpGet("")]
public async Task<UserViewModel> Get(int id)
[HttpGet("")]
public async Task<UserViewModel> GetByName(string name)
[HttpGet("")]
public async Task<UserViewModel> GetByUserName(string name)

//You can access like 
//- api/Users/
//- api/Users/?id=123
//- api/Users/?name=someneme
//- api/Users/?username=someneme
Sign up to request clarification or add additional context in comments.

4 Comments

Excellent! It's working now. Thanks for that ... i don't know what I've done wrong on my code :p Taking the opportunity, is there any benefit to use public async Task<UserViewModel> Get(int id) rather than public UserViewModel Get(int id)? as far as i know web api will use task on the pipeline anyway. cheers
May be this help... hanselminutes.com/327/…
Thanks jd4u :D I've missed this episode, very helpful indeed. Cheers
Should the last example be GetByUserName(string username) or is some convention on the method name search matching the querystring with ?username=someone to the method name GetByUserName? In other words, I do not want the method name to force what the querystring param key should be; instead it should come from the method param name.

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.