Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle C# .NET GET / POST?

Tags:

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

like image 420
tripbrock Avatar asked Jun 10 '11 13:06

tripbrock


People also ask

What is handle in C?

A handle is a generic term for a reference (not specifically a C++ reference) to an object. A pointer is a subset of handle, since it points to objects. A foreign key in a database is also a handle, since it points to records in other tables; and it is not a pointer.

Is C very difficult?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

Is C easy for beginners?

Which programming language is easy to learn? C and C++ are both somewhat difficult to learn to program well. However, in many respects, they share many similarities with many other popular languages. In that sense they're just as easy (or as difficult) to learn, at first, as anything other programming language.


2 Answers

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.

like image 165
Patrick Desjardins Avatar answered Sep 27 '22 19:09

Patrick Desjardins


Basically that is:

var request = Request["q"];         // $_REQUEST
var post = Request.Form["q"];       // $_POST
var get = Request.QueryString["q"]; // $_GET
like image 44
BrunoLM Avatar answered Sep 27 '22 20:09

BrunoLM