25

I am trying to get POST data, but I'm having no luck. My code is below. When I click the form button nothing happens.

I expected at least my IDE to snap at A.Ret(), but nothing happens whatsoever.

File Test.cs

using System.Web;
public class A
{
    public static string ret() {
        var c = HttpContext.Current;
        var v = c.Request.QueryString; // <-- I can see get data in this
        return c.Request.UserAgent.ToString();
        return c.Request.UserHostAddress.ToString();
        return "woot";
    }
}

File Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="aspnetCSone._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>

    <body>
        <form id="form1" runat="server" method="post" action="Default.aspx">
            <input type=hidden name="AP" value="99" />
            <input type=button value="Submit" />
            <div>
                <a id="aa">a</a>
                <% = A.ret() %>
            </div>
        </form>
    </body>
</html>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I'm not sure I follow... Where are you trying to get POST data? – Blixt Jun 10 '09 at 16:13
  • This code makes no sense. What are you trying to achieve? – Michał Chaniewski Jun 10 '09 at 16:15
  • Don't try to get POST data, mark the control as runat="server" and then get the value that way. – rball Jun 10 '09 at 16:16
  • rball: How do i get the value 'that' way? I am an utter newbie. Blixt: The form should be POSTing the data when i click submit. Like how it does in PHP and all HTML code i have done before today. –  Jun 10 '09 at 16:27

4 Answers4

43

Try using:

string ap = c.Request["AP"];

That reads from the cookies, form, query string or server variables.

Alternatively:

string ap = c.Request.Form["AP"];

to just read from the form's data.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
32

c.Request["AP"] will read posted values. Also you need to use a submit button to post the form:

<input type="submit" value="Submit" />

instead of

<input type=button value="Submit" />
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I realize this is way old, but it helped me too... though I was using VB... 'Dim sku As String = Request("sku")'... thanks! –  Sep 25 '12 at 20:01
5

I'm a little surprised that this question has been asked so many times before, but the most reuseable and friendly solution hasn't been documented.

I often have webpages using AngularJS, and when I click on a Save button, I'll "POST" this data back to my .aspx page or .ashx handler to save this back to the database. The data will be in the form of a JSON record.

On the server, to turn the raw posted data back into a C# class, here's what I would do.

First, define a C# class which will contain the posted data.

Supposing my webpage is posting JSON data like this:

{
    "UserID" : 1,
    "FirstName" : "Mike",
    "LastName" : "Mike",
    "Address1" : "10 Really Street",
    "Address2" : "London"
}

Then I'd define a C# class like this...

public class JSONRequest
{
    public int UserID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
}

(These classes can be nested, but the structure must match the format of the JSON data. So, if you're posting a JSON User record, with a list of Order records within it, your C# class should also contain a List<> of Order records.)

Now, in my .aspx.cs or .ashx file, I just need to do this, and leave JSON.Net to do the hard work...

    protected void Page_Load(object sender, EventArgs e)
    {
        string jsonString = "";
        HttpContext.Current.Request.InputStream.Position = 0;
        using (StreamReader inputStream = new StreamReader(this.Request.InputStream))
        {
            jsonString = inputStream.ReadToEnd();
        }
        JSONRequest oneQuestion = JsonConvert.DeserializeObject<JSONRequest>(jsonString);

And that's it. You now have a JSONRequest class containing the various fields which were POSTed to your server.

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
1

The following is OK in HTML4, but not in XHTML. Check your editor.

<input type=button value="Submit" />
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
e-info128
  • 3,727
  • 10
  • 40
  • 57