Retrieving data from a POST method in ASP.NET
Solution 1
You can get a form value posted to a page using code similiar to this (C#) -
string formValue;
if (!string.IsNullOrEmpty(Request.Form["txtFormValue"]))
{
formValue= Request.Form["txtFormValue"];
}
or this (VB)
Dim formValue As String
If Not String.IsNullOrEmpty(Request.Form("txtFormValue")) Then
formValue = Request.Form("txtFormValue")
End If
Once you have the values you need you can then construct a SQL statement and and write the data to a database.
Solution 2
The data from the request (content, inputs, files, querystring values) is all on this object HttpContext.Current.Request
To read the posted content
StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream);
string requestFromPost = reader.ReadToEnd();
To navigate through the all inputs
foreach (string key in HttpContext.Current.Request.Form.AllKeys)
{
string value = HttpContext.Current.Request.Form[key];
}
Etienne
Updated on December 13, 2020Comments
-
Etienne over 3 years
I am using ASP.NET.
There is a system that needs to POST data to my site and all they asked for is for me to provide them with a URL. So I gave them my URL http://www.example.com/Test.aspx.
Now I do not know exactly how they POST it but now on my Test.aspx page I need to write code that will save that data to a database.
But how would this work and what must I do on my Test.aspx page?
I wrote some code in my Page Load Event that sends me an email on Page Load to see if they actually hit the page and it does not seem like they are even?
-
Etienne over 12 yearsSo txtFormValue will be one of the parameters that gets passed to my URL?
-
ipr101 over 12 yearsYes it will - you'd need to adjust the names of the of the values you were checking for in the
Request.Form
collection depending on what was being posted to you. -
Etienne over 12 yearsBut must I place this code in my PAGE LOAD event? Does it even load the page when a POST to that URL occurs?
-
ipr101 over 12 yearsPAGE LOAD would be a good place to put the code as that event should fire when data is posted to the page. You could test a post to your page using a tool such as Fiddler.
-
Kasaku over 12 years@Etienne, all steps of the page lifecycle (Of which
Load
is one) are called regardless of GET or POST. Some additional ones are also invoked on POST. Would strongly suggest you read the ASP.NET Page Life Cycle Overview to understand how ASP.NET works before progressing much further. -
Etienne over 12 years@PirateKitten, is there a server or web.config setting I need to set to allow POST to my website/page?
-
Kasaku over 12 yearsThere isn't. I would echo @TJHeuvel above and strongly suggest you buy an ASP.NET book to read up on the subject, because by the nature of your questions there will be too much to cover in comments.
-
Sunil Kumar almost 6 yearsNice one, can we map it with an entity(table) in entity framework??
-
Adrian Iftode almost 6 yearsMaybe try MVC or Web Api to do the job? :D
-
Sunil Kumar almost 6 yearsI am using Web API but don't know how to map entity?