public static
public static

Reputation: 12990

Acessing querystring and form parameters with Web API

If you choose for whatever reason not to use modelbinding in a HttpPost request, what other ways are there to access the QueryString (HttpGet) or Form parameters (HttpPost)?

Traditionally you could do:

Request.QueryString["Key"]
Request.Form["Key"]
Request["Key"]

I can't seem to find anything similiar in Web API.

Upvotes: 8

Views: 3025

Answers (3)

Md Manzoor Ahmed
Md Manzoor Ahmed

Reputation: 29

var queryString = request.RequestUri.ParseQueryString();
token = queryString["Authorization"];

Upvotes: 2

Marcel N.
Marcel N.

Reputation: 13976

For query string parameters you can use GetQueryNameValuePairs on a HttpRequestMessage (it's an extension method).

For form data, you need to define the action as this and the raw form data (pre-parameter binding) will be passed to you:

public void Post(NameValueCollection formData)
{
   var value = formData["key"];
}

Upvotes: 8

Chris Pratt
Chris Pratt

Reputation: 239220

This is where Intellisense comes in handy. Just type Request. and see what you've got available to you. I've personally always just included parameters for my method for the data to bind to; not really sure what use case exists where you wouldn't just handle it that way. Nevertheless, from what I can see, there's Request.GetQueryNameValuePairs that will at least let you get at the query string. I can't see anything in Request that gives you access to the post body, but perhaps I missed it, or it's buried somewhere else other than Request.

Upvotes: 0

Related Questions