Hodza  Ndzaho
Hodza Ndzaho

Reputation: 45

Web Api Routing conflict

There is a web api config problem with the parameter types. I need to recognize two type of parameters, one is int and other is boolean. In WebApi config default route is setted as "api/{controller}/{id}", and when i parse integer it is OK, but i need also to parse boolean parameter. So when I go to /api/controller/1 url, i need it to go to the Action with int input parameter, and when I go to /api/controller/{true|false} url, I need it to go to Action where input is boolean.

Any solutions? Thx

Upvotes: 0

Views: 1023

Answers (1)

Amirhossein Mehrvarzi
Amirhossein Mehrvarzi

Reputation: 18974

Use Route or RoutePrefix filter attributes as mentioned in Attribute Routing in Web Api 2 and Create a REST API with attribute routing.

Since I don't know the whole structure of your api controller and its methods, I can just put two examples for above attributes here. Generally, We place the Route attribute to filter requests related to a controller. In your question can be something like this:

[RoutePrefix("api/{controller}")]

And to filter specific requests for each method, we use Route attribute before each method, for example in your question it can be some thing like bellow line of code for a boolean input method:

[Route("{id:bool}")]

You need to know some rules to employ mentioned attributes as I placed 2 links.

Upvotes: 3

Related Questions