Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing based on query string parameter name

I'm trying to configure routing in my MVC4 WebAPI project.

I want to be able to search for products based on their name or their type like so:

/api/products?name=WidgetX - returns all products named WidgetX /api/products?type=gadget - returns all products of type gadget

The routes are configured like this:

config.Routes.MapHttpRoute(     name: "Get by name",     routeTemplate: "api/products/{name}",     defaults: new { controller = "ProductSearchApi", action = "GetProductsByName", name = string.Empty } );  config.Routes.MapHttpRoute(     name: "Get by type",     routeTemplate: "api/products/{type}",     defaults: new { controller = "ProductSearchApi", action = "GetProductsByType", type = string.Empty } ); 

The problem is that the name of the query string parameter seems to be ignored so the first route is always the one used, regardless the name of the query string parameter. How can I modify my route to get it right?

like image 231
rickythefox Avatar asked Sep 27 '12 10:09

rickythefox


People also ask

What is a query string parameter name?

Updated: 11, Sat, 2020 07:50 PM. On the internet, a Query string is the part of a link (otherwise known as a hyperlink or a uniform resource locator, URL for short) which assigns values to specified attributes (known as keys or parameters).

How do I find my router query parameters?

import ActivatedRoute from '@angular/router'. Inject ActivatedRoute class in constructor. Access queryParams property of ActivatedRoute class which returns an observable of the query parameters that are available in the current URL route.

What is the difference between a query parameter and a route parameter?

The key difference between query parameters and route parameters is that route parameters are essential to determining route, whereas query parameters are optional.

What is path parameter and query parameter example?

URI parameter (Path Param) is basically used to identify a specific resource or resources whereas Query Parameter is used to sort/filter those resources. Let's consider an example where you want identify the employee on the basis of employeeID, and in that case, you will be using the URI param.


2 Answers

What you need is just only one route below because query string is not used as routing parameters:

config.Routes.MapHttpRoute(     name: "Get Products",     routeTemplate: "api/products",     defaults: new { controller = "ProductSearchApi" } ); 

And, then define two methods like below:

GetProductsByName(string name) {}  GetProductsByType(string type) {} 

Routing mechanism is smart enough to route your url to your correct action based on the name of query string whether the same with input parameters. Of course on all methods with prefix are Get

You might need to read this: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection

like image 51
cuongle Avatar answered Sep 30 '22 06:09

cuongle


You don't need to include your query parameters in the route. There should only be one simple route map to cover the Http Methods on all of your ApiControllers:

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

The only time you need to adjust the routes is if you want to move a parameter into the actual path, which you don't seem to be doing. Then your GET http method to search by two fields would be:

public IEnumerable<Product> Get(string name, string type){     //..your code will have to deal with nulls of each parameter } 

If you want to explicitly search by one field at a time then you should think about using different controllers for different purposes. Ie, a SearchProductByTypeController that has a single Get(string type) method. The route would then be /api/SearchProductByTypeController?type=gadget

like image 22
Nick Avatar answered Sep 30 '22 06:09

Nick