1

I'm struggling to get some Web Api 2.2 routing working correctly. I have a route:

config.Routes.MapHttpRoute(
            name: "OrgAdminGetOrgUsers",
            routeTemplate: "organisations/{id}/users",
            defaults: new { controller = "OrganisationDetails", action = "GetUsersInAnOrganisation" }                
        );

The problem I'm having is that when I add a query string for making the GET searchable, my routing stops working and I get 404's. I've looked everywhere for examples of using both Uri parameters and a query string, but I can't find anything. It seems like something people would do a lot though? When I remove the query string and the optional parameters from the controller the routing works fine.

Uri:

organisations/3/users?orderby=asc&orderByColumn=surname&start=1&end=15 

Controller:

[HttpGet]
    public IEnumerable<User> GetUsersInAnOrganisation(int id, string email = "", string firstName = "", string surname = "", string orderByColumn = "", string orderBy = "asc", int start = -1, int end = -1)

Many thanks in advance for any help!

2 Answers 2

1

So I've managed to get it working, the controller needed to be declared with [FromUri] for the id parameter:

[HttpGet]
public IEnumerable<User> GetUsersInAnOrganisation([FromUri] int id, string email = "", string firstName = "", string surname = "", string orderByColumn = "", string orderBy = "asc", int start = -1, int end = -1)
Sign up to request clarification or add additional context in comments.

Comments

0

This worked for me -

the config -

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            "Default", 
            "api/{controller}/{action}/{id}", 
            defaults: new { id = RouteParameter.Optional });

the url with query params -

http://localhost:57597/api/OrganisationDetails/GetUsersInAnOrganisation/3?
orderby=asc&orderByColumn=surname&start=1&end=15

enter image description here

Hope this is helpful.

1 Comment

Hi, I don't really want to use that url template, I want to have organisations/{id}/users as this style of uri (having a parameter in the middle of it) will be used throughout the api.

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.