1

I have WebAPI url (http://localhost:5134/api/TechDisci/00026) which returns multiple TechDisciplines for user 00026. /TechDisci/ is the controller name and Get method returns JSON data.

JSON data has primary and secondary values in the results set. Now User wants to filter this with primary and secondary by passing in url like this

http://localhost:5134/api/TechDisci/00026/primary and http://localhost:5134/api/TechDisci/00026/secondary.

My java guys are able to this kind of url thing. How to handle samethig in WebAPI?

1 Answer 1

1

You can have "primary" and "secondary" as action (method) names in the controller. Then you will need to add a new route (in class Application.WebApiConfig).

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}/{action}"
);

Or if "primary" and "secondary" are parameters to the same method. Then you might add this route

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

In this case you will need to add an overload to your Get method that accepts two parameters (id, priority)

In both cases add the route before the main route, just in case to make sure it is the first one that has a match.

Sign up to request clarification or add additional context in comments.

Comments

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.