Recognizing request and form parameters using the Web API

If for some reason you decide not to use model binding in an HttpPost request, what are other ways to access QueryString (HttpGet) or form (HttpPost) parameters?

Traditionally you can:

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

I can not find anything similar in the web API.

+7
c # asp.net-web-api
source share
3 answers

For query string parameters, you can use GetQueryNameValuePairs in HttpRequestMessage (this is an extension method).

For form data, you need to define an action like this, and you will be given raw form data (pre-binding parameters):

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

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

+1
source share

This is where Intellisense comes in handy. Just enter Request. and find out what you have. I personally always included parameters for my method for data binding; not quite sure which use case exists where you are not just dealing with it. However, from what I see, there is Request.GetQueryNameValuePairs , which, at least, will allow you to get a query string. I do not see anything in Request that gives you access to the body of the message, but maybe I missed it or buried it somewhere else besides Request .

0
source share

All Articles