34

I feel a bit absurd asking this but I can't find a way to get parameters for a get request at /api/foo?sort=name for instance.

In the ApiController class, I gave a public string Get(). Putting Get(string sort) makes /api/foo a bad request. Request instance in the ApiController is of type System.Net.Http.HttpRequestMessage. It doesn't have a QueryString or Parameters property or anything.

6
  • Is this your get/id or are you trying to do your get? For a get without id return an IQueryable. You will need both a GET() and a GET(id) if you want to fully support REST. Commented May 18, 2012 at 17:15
  • Sorry, should have specified. I'm trying to create an API to return a json whose content is influenced by the get?parameter. I'm not quite exposing the data source directly via the API Commented May 18, 2012 at 17:18
  • If you need to evaluate on more than the id then you will need to go IQueryable and use Odata. Commented May 18, 2012 at 17:27
  • If you have Get(string sort), then api/foo and api/foo?sort=name should both route to your Get method. Are you saying you get a 400 error? Commented May 20, 2012 at 6:04
  • Rick Strahl has a great blog about this with some extensions that I ended up using... Commented Apr 6, 2015 at 20:03

8 Answers 8

35

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

GetQueryNameValueParirs() was not available on the Request object. It worked after "using System.Net.Http;"
21

You could just use

HttpContext.Current.Request.QueryString

1 Comment

this doesn't work in the context of ApiController in mvc3
7

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

2 Comments

Or even better: var q = Request.GetQueryNameValuePairs().SingleOrDefault(nv => nv.Key =="q");
You're right, its nicer to write it that way, but in my case add the Select(nv => nv.Value) to it
5

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();

Comments

4

You can also use the following

var value = request.GetQueryNameValuePairs().Where(m => m.Key == "paramName").SingleOrDefault().Value;

1 Comment

This might not work as intended when the default value is returned for a KeyValuePair when immediately using .Value on it.
3

if we have a proper model for that request

for example

  public class JustModel 
    {
      public int Id {get;set;}
      public int Age {gets;set;}
    }

and query like this

/api/foo?Id=1&Age=10

You could just use [FromUri] attribute

For example

public IHttpActionResult GetAge([FromUri] JustModel model){}

Comments

1

Adding a default value does the job:

public string Get(string sort="")

Comments

0

You're trying to build an OData webservice? If so, just return an IQueryable, and the Web API will do the rest.

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.