8

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.

3 Answers 3

8

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

Comments

2

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

Comments

0

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.

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.