I'm still not quite sure how to do this in the Web API controller. I'm basically trying to obtain all form values that were posted to my API. So for example, if a web form has a firstname and lastname textbox, and they post these values to the api, how
to I obtain these values on the api side? I guess i'm trying to figure out how to do the webform equivalent of Request.Form["firstname"]...but in the API.
If your form uses application/x-www-form-urlencoded, you can put the form controls as action parameters, like digitalpacman mentioned. You can also get them from the Request object:
var qs = this.Request.RequestUri.ParseQueryString();
string name = qs["firstname"];
First a bit more info which i should have included in the first place. First note that this example is just a simplified version of what i'm actually trying to do.
- So first, I'm posting the form data from a codebehind file using c# (not from an html page or via json).
- My uri is /api/newUser
- My route for the action looks pretty much the same: "api/newUser"
- My controller action looks like this: publicHttpResponseMessage<Contact>PostUser()
So in regards to digitalpacman's response of just adding a param to the action, that is fine, but in order to do that, won't that force me to change my uri and route? (I'm trying to keep the uri as /api/newUser). So instead of posting to a uri of /api/newUser,
wouldn't this force my uri to be "api/newUser/firstName".
Also, i tried to post via the codebehind using the application/x-www-form-urlencoded type, but without success. So i'm doing something like:
-----
HttpWebRequest req = null;
HttpWebResponse res = null;
const string url = "http://localhost:8090/api/newUser";
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.Headers.Add("MyRequest", url);
req.ContentLength = 900;
var sw = new StreamWriter(req.GetRequestStream());
sw.Write("testing");
sw.Close();
res = (HttpWebResponse)req.GetResponse();
Stream responseStream = res.GetResponseStream();
var streamReader = new StreamReader(responseStream);
//Read the response into an xml document
var soapResonseXmlDocument = new XmlDocument();
soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());
apiResponse.Text = soapResonseXmlDocument.InnerXml;
jjonesca
Member
2 Points
7 Posts
How to obtain POST Request values in the API
Apr 24, 2012 06:05 PM|LINK
I'm still not quite sure how to do this in the Web API controller. I'm basically trying to obtain all form values that were posted to my API. So for example, if a web form has a firstname and lastname textbox, and they post these values to the api, how to I obtain these values on the api side? I guess i'm trying to figure out how to do the webform equivalent of Request.Form["firstname"]...but in the API.
Thanks in advance.
digitalpacma...
Member
183 Points
148 Posts
Re: How to obtain POST Request values in the API
Apr 24, 2012 06:14 PM|LINK
You put the parameters in the action.
DoSomething(string firstName, string lastName)
It uses the MVC model binder, googling will give you tons of articles.
digitalpacma...
Member
183 Points
148 Posts
Re: How to obtain POST Request values in the API
Apr 24, 2012 06:17 PM|LINK
Also you arn't supposed to use request.form in webforms. You are supposed to use the controls directly like txtFirstName.Text
Request.Form is for classic ASP and unique dynamic cases.
MikeWasson
Member
499 Points
79 Posts
Microsoft
Re: How to obtain POST Request values in the API
Apr 24, 2012 06:39 PM|LINK
If your form uses application/x-www-form-urlencoded, you can put the form controls as action parameters, like digitalpacman mentioned. You can also get them from the Request object:
If your form uses multipart/form-data, it's somewhat more complicated. See http://www.asp.net/web-api/overview/formats-and-model-binding/html-forms-and-multipart-mime
jjonesca
Member
2 Points
7 Posts
Re: How to obtain POST Request values in the API
Apr 24, 2012 08:03 PM|LINK
First a bit more info which i should have included in the first place. First note that this example is just a simplified version of what i'm actually trying to do.
- So first, I'm posting the form data from a codebehind file using c# (not from an html page or via json).
- My uri is /api/newUser
- My route for the action looks pretty much the same: "api/newUser"
- My controller action looks like this: public HttpResponseMessage<Contact> PostUser()
So in regards to digitalpacman's response of just adding a param to the action, that is fine, but in order to do that, won't that force me to change my uri and route? (I'm trying to keep the uri as /api/newUser). So instead of posting to a uri of /api/newUser, wouldn't this force my uri to be "api/newUser/firstName".
Also, i tried to post via the codebehind using the application/x-www-form-urlencoded type, but without success. So i'm doing something like:
-----
HttpWebRequest req = null; HttpWebResponse res = null; const string url = "http://localhost:8090/api/newUser"; req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.Headers.Add("MyRequest", url); req.ContentLength = 900; var sw = new StreamWriter(req.GetRequestStream()); sw.Write("testing"); sw.Close(); res = (HttpWebResponse)req.GetResponse(); Stream responseStream = res.GetResponseStream(); var streamReader = new StreamReader(responseStream); //Read the response into an xml document var soapResonseXmlDocument = new XmlDocument(); soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd()); apiResponse.Text = soapResonseXmlDocument.InnerXml;-----
Any clues as to what i'm doing wrong?
jjonesca
Member
2 Points
7 Posts
Re: How to obtain POST Request values in the API
Apr 24, 2012 08:30 PM|LINK
Would i need to basically append all of the data from the from (in the codebehind page) onto the end of the uri as a querystring? For example:
protected void Page_Load(object sender, EventArgs e) { NameValueCollection formValues; string uri = "/api/newUser" if(IsPostBack) { formValues = Request.Form; string formdata = formValues.ToString(); if(formdata != "") uri += "?formdata"; } }Is this the only way I can submit the formdata (via a codebehind page)? I'm hoping not...