22

As I'm new to .NET after coming from PHP I chose C# to work with and its coming along nicely. I have a question though regarding the handling of GET and POST.

So far I've established that I can put this in the codefile behind the aspx page:

if (Request.HttpMethod.ToString() == "POST") {

    Response.Write("You sent a post!")

}

and I could and an ELSE there to handle a GET, but how exactly would you do that?

In PHP I would do something like this:

Example URL = http://www.example.com/page.php?foo=bar

$foobar = $_GET['foo'];

Could some kind soul please give me pointers on dealing with this in C#.

Thanks

tripbrock
  • 952
  • 4
  • 13
  • 28
  • I would recommend working with MVC3, you can specifically tag your controller actions whether you expect them to be get, post, or any. – Chris Marisic Jun 10 '11 at 13:16
  • 1
    Why not use ASP.NET MVC instead of Webforms? ASP.NET MVC doesn't try to abstract away the nature of the web. – George Stocker Jun 10 '11 at 13:17
  • Are you asking how you access the parameters being sent as part of either the POST or the GET? – The Evil Greebo Jun 10 '11 at 13:18
  • Its a half finished project I've inherited as it would be a "good starting point" according to the senior dev. Its a mix of classic ASP and .NET. Sadly PHP gets outvoted every time by the other devs which is a shame. Reading up on the MVC approach I'd probably use that for a scratch project. – tripbrock Jun 10 '11 at 13:48

6 Answers6

34

The .Net version of $_GET[] is :

 Request.QueryString["parameter1"]

You do not require to do this IF condition.

The .Net version of $_POST[] is :

 Request.Form["paramName"];

Still no need the IF condition.

BUT in Asp.Net webform you do not require to use all the time Request class because the PostBack to the page will contain your form data directly into the control value. Let say you have a textbox called txt1, when the user will submit the form you can get the value of this textbox directly by accessing txt1.

inorganik
  • 24,255
  • 17
  • 90
  • 114
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
11

Basically that is:

var request = Request["q"];         // $_REQUEST
var post = Request.Form["q"];       // $_POST
var get = Request.QueryString["q"]; // $_GET
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
6

Try

    string foobar = Request.QueryString["foo"];
Bala R
  • 107,317
  • 23
  • 199
  • 210
3

If you are looking to get the query string value of Foo use:

Request.QueryString["foo"];

You can use the request object to get values posted to your page.

Miyagi Coder
  • 5,464
  • 4
  • 33
  • 42
3

If your objective is to be able to access the parameters being passed in regardless of the method used (get vs. post) then you can just use Request.Params["paramname"] to access them, and you don't need to worry about whether it was a get or a post.

The Evil Greebo
  • 7,013
  • 3
  • 28
  • 55
1

Pardon me if I am not quite understanding the question but I believe you are asking for the QueryString property?

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

Mike Cheel
  • 12,626
  • 10
  • 72
  • 101