62

I'm using the default template generated by Asp.net Web Api. I'm working with the Get() part:

// GET api/values
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}

For some reason I thought the only thing you had to do to access to query string was just to create an input string variable. So I created one more function (the only change I made) to the default controller generated:

public IEnumerable<string> Get(string queryString)
{
    return new string[] { "value3", "value4" };
}

I put a breakpoint in both methods but even if I add a query string it always goes to the function with no parameters. So if I go to http://mybaseurl/api/values?foo=f it still is going to Get() instead of Get(string queryString). Does it not work the way I thought? I know I can access the query string in the Get() function using Request.RequestUri.ParseQueryString(); but I prefer to have it separated like this if possible.

4 Answers 4

69

Even though @Kiran Challa's answer is correct, there are few situations that you might prefer to get URL parameters directly from the URL. in those scenarios, try this:

using System.Net.Http;

var allUrlKeyValues = ControllerContext.Request.GetQueryNameValuePairs();

string p1Val = allUrlKeyValues.LastOrDefault(x => x.Key == "p1").Value;
string p2Val = allUrlKeyValues.LastOrDefault(x => x.Key == "p2").Value;
string p3Val = allUrlKeyValues.LastOrDefault(x => x.Key == "p3").Value;

Now for the following URL, p1Val will be "Apple", p2Val will be "Banana", and p3Val will be null.

.../api/myController?p1=Apple&p2=Banana

Update:

Thanks for the suggestions, now, the source code for this test is in GitHub and it also runs and can be tested on Azure:

Sign up to request clarification or add additional context in comments.

2 Comments

I get an error 500 if I specify the same query name twice - this is permitted in HTTP (names can have multiple values) but it makes freewebapi crash: freewebapi.azurewebsites.net/api/…
You are right @Dai Have to improve the code to avoid that exception. How about replacing .SingleOrDefault() with .FirstOrDefault() or .LastOrDefault() ?
53

The query string key name should match the parameter name of the action:

/api/values?queryString=f

public IEnumerable<string> Get(string queryString)
    {
        return new string[] { "value3", "value4" };
    }

1 Comment

if there in url we have ?$callback how can fetch that
4

There is one more way to get query string as shown below when you are using wildcard in route or for dynamic routes.

string query = ControllerContext.HttpContext.Request.QueryString.Value;

BONUS:)

string path = ControllerContext.HttpContext.Request.Path.ToUriComponent();

Comments

1

This will also get you a specific query parmeter by name:

string testValue = request.GetQueryParameterDictionary().LastOrDefault(x =>
x.Key == "test").Value;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.