c# - How do I create a truly multi-purpose endpoint using WebAPI 2 with optional parameters? -
thanks looking.
i attempting make following code work:
[routeprefix("api/user")] public class usercontroller : apicontroller { [allowanonymous] [route("{email}")] public ihttpactionresult get(string email = null) { if (!string.isnullorempty(email)) { return json(someuserprofileobject); } return json(somelistofuserprofiles); } }
whenever try , access /api/user/some@email.com, 405
.
solved!
please see solution in answer below.
i finish composing question when stumbled on answer. basically, special characters don't work in url request paths. used query string instead , revised code this:
[routeprefix("api/user")] public class usercontroller : apicontroller { [allowanonymous] [route("")] public ihttpactionresult get(string email = null) { if (!string.isnullorempty(email)) { return json(someuserprofileobject); } return json(somelistofuserprofiles); } }
now works fine , can use endpoint both lists of users individual user if querystring provided.
cheers!
Comments
Post a Comment