I was facing the same issue of 'How to include search parameters as a query string?', while I was trying to build a web api for my current project. After googling, the following is working fine for me:

Api controller action:

[HttpGet, Route("search/{categoryid=categoryid}/{ordercode=ordercode}")]

public Task<IHttpActionResult> GetProducts(string categoryId, string orderCode)
{

}

The url I tried through postman:

http://localhost/PD/search?categoryid=all-products&ordercode=star-1932

http://localhost/PD is my hosted api

After much painstaking fiddling and Googling, I've come up with a 'fix'. I don't know if this is ideal/best practice/plain old wrong, but it solves my issue.

All I did was add [Route("")] in addition to the route attributes I was already using. This basically allows Web API 2 routing to allow query strings, as this is now a valid Route.

An example would now be:

[HttpGet]
[Route("")]
[Route("{name}/{drink}/{sport?}")]
public List<int> Get(string name, string drink, string sport = "")
{
    // Code removed...
}

This makes both localhost:12345/1/Names/Ted/coke and localhost:12345/1/Names?name=Ted&drink=coke valid.


With the Attribute routing you need to specify default values so they would be optional.

[Route("{name}/{sport=Football}/{drink=Coke}")]

Assigning a value will allow it to be optional so you do not have to include it and it will pass the value to specify.

I have not tested the query string for this but it should work the same.

I just re-read the question and I see that you have 2 Get verbs with the same path, I believe this would cause conflict as routing would not know which one to utilize, perhaps using the optional params will help. You can also specify one can be null and do checking in the method as to how to proceed.

[Route("{name}/{sport?}/{drink?}")]

Then check the variables in the method to see if they are null and handle as needed.

Hope this helps, some? lol

If not perhaps this site will, it has more details about attribute routing.

http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

Clip from that site:

Optional parameters and default values You can specify that a parameter is optional by adding a question mark to the parameter, that is:

[Route("countries/{name?}")]
public Country GetCountry(string name = "USA") { }

Currently, a default value must be specified on the optional parameter for action selection to succeed, but we can investigate lifting that restriction. (Please let us know if this is important.)

Default values can be specified in a similar way:

[Route("countries/{name=USA}")]
public Country GetCountry(string name) { }

The optional parameter '?' and the default values must appear after inline constraints in the parameter definition.


Just a side note from my part as well. In order for queryString params to work, you need to provide a default value for your method parameters to make it optional. Just as you would also do when normally invoking a C# method.

[RoutePrefix("api/v1/profile")]
public class ProfileController : ApiController
{

   ...

   [HttpGet]
   [Route("{profileUid}")]
   public IHttpActionResult GetProfile(string profileUid, long? someOtherId) 
   {
      // ...
   }

   ...

}

This allows me to call the endpoint like this:

/api/v1/profile/someUid
/api/v1/profile/someUid?someOtherId=123