36

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?

0

3 Answers 3

43

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];
}
3
  • 1
    Nice one, can we map it with an entity(table) in entity framework?? Commented Jun 28, 2018 at 12:04
  • 1
    Maybe try MVC or Web Api to do the job? :D Commented Jun 28, 2018 at 12:19
  • I am using Web API but don't know how to map entity? Commented Jun 28, 2018 at 12:22
31

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.

7
  • So txtFormValue will be one of the parameters that gets passed to my URL?
    – Etienne
    Commented Sep 28, 2011 at 8:10
  • Yes 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.
    – ipr101
    Commented Sep 28, 2011 at 8:12
  • But must I place this code in my PAGE LOAD event? Does it even load the page when a POST to that URL occurs?
    – Etienne
    Commented Sep 28, 2011 at 8:17
  • PAGE 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.
    – ipr101
    Commented Sep 28, 2011 at 8:21
  • 1
    @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.
    – Kasaku
    Commented Sep 28, 2011 at 8:58
0

You need to examine (put a breakpoint on / Quick Watch) the Request object in the Page_Load method of your Test.aspx.cs file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.