Direct access to data

I have an action in one of my controllers that will receive HTTP POST requests from outside my MVC website.

All these POST requests will have the same parameters, and I will need to parse the parameters.

How can I access post data from an action?

This is a potentially very simple question!

thank

+46
asp.net-mvc
May 4 '11 at
source share
6 answers

POST data from your HTTP requests can be obtained in Request.Form .

+51
May 04 '11 at 15:00
source share
— -
 string data = new System.IO.StreamReader(Request.InputStream).ReadToEnd(); 
+34
Nov 19 '12 at 5:37
source share

Use

 Request.InputStream 

This will give you raw access to the body of the HTTP message, which will contain all the POST variables.

http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx

+18
May 4 '11 at 15:05
source share

I tried to access the POST data after being inside the MVC controller. The InputStream was already dealing with the controller, so I needed to reset the InputStream to 0 to read it again.

This code worked for me ...

  HttpContext.Current.Request.InputStream.Position = 0; var result = new System.IO.StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd(); 
+12
Apr 19 '14 at 16:45
source share
 Stream req = Request.InputStream; req.Seek(0, System.IO.SeekOrigin.Begin); string json = new StreamReader(req).ReadToEnd(); JavaScriptSerializer serializer = new JavaScriptSerializer(); dynamic items = serializer.Deserialize<object>(json); string id = items["id"]; string image = items["image"]; 

/// you can access the parameters by name or index

+4
Mar 20 '16 at 11:00
source share

The web server does not have to worry about where the request comes from. If your client application has an input control called a username and it is sent to your application, it will perceive the same as your sent one if it is from your own application with a username.

One huge caveat is if you have implemented AntiForgeryValidation, which is why a big headache will allow you to publish an external form.

+3
May 4 '11 at 3:13
source share



All Articles