How to get GET parameters with ASP.NET MVC ApiController

The ApiController is designed to work without the HttpContext object (making it portable, and allowing it to be hosted outside of IIS).

You can still access the query string parameters, but it is done through the following property:

Request.GetQueryNameValuePairs()

Here's an example loop through all the values:

foreach (var parameter in Request.GetQueryNameValuePairs())
{
     var key = parameter.Key;
     var value = parameter.Value;
}

Get all querystring name/value pairs into a variable:

IEnumerable<KeyValuePair<string, string>> queryString = request.GetQueryNameValuePairs();

Then extract a specified querystring parameter

string value = queryString.Where(nv => nv.Key == "parameterNameGoesHere").Select(nv => nv.Value).FirstOrDefault();

You could just use

HttpContext.Current.Request.QueryString

Here's an example that gets the querystring q from the request and uses it to query accounts:

        var q = Request.GetQueryNameValuePairs().Where(nv => nv.Key =="q").Select(nv => nv.Value).FirstOrDefault();
        if (q != null && q != string.Empty)
        {
            var result = accounts.Where(a=>a.Name.ToLower().StartsWith(q.ToLower()));
            return result;
        }
        else
        {
            throw new Exception("Please specify a search query");
        }

This can be called then like this:

url/api/Accounts?q=p