Name: <?php print $_POST['nameField']; ?> <br/>
Age: <?php print $_POST['ageField']; ?> <br/>
Comment: <?php print $_POST['commentField']; ?> <br/>

How do I do the same thing in C#?

Thanks :icon_question:

Dani AI

Generated

As discovered, ASP.NET exposes posted form values on the server through the request object. The WebForms request exposes a Form collection (a NameValueCollection) you can read from; in MVC you typically rely on model binding instead for cleaner, typed parameters. Below are compact patterns and a few practical tips.

// WebForms (code-behind)
protected void Page_Load(object sender, EventArgs e)
{
    // values come back as strings (null if missing)
    string name = Request.Form["nameField"];
    int age;
    int.TryParse(Request.Form["ageField"], out age);
    string comment = Server.HtmlEncode(Request.Form["commentField"] ?? "");
    string[] choices = Request.Form.GetValues("multiSelectName"); // multiple values
}
// ASP.NET MVC (preferred for POST forms)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Submit(string nameField, int ageField, string commentField)
{
    var safeComment = Html.Encode(commentField);
    // model binding handles parsing and validation better than manual Request reads
}

Notes and troubleshooting: check Request.HttpMethod == "POST" if a page handles both GET and POST. Always validate and encode user input before rendering to avoid XSS (see HtmlEncode). Use Request.Form.GetValues for repeated controls (checkbox lists) and Request.Files for uploads. If reading POST data outside a Page/Controller, use HttpContext.Current.Request. Prefer explicit Request.Form over the generic Request[] lookup to avoid accidentally mixing QueryString values. Official references: HttpRequest.Form, HttpUtility.HtmlEncode, and Model binding in ASP.NET Core.

Someone gave me the solution

I use "Request.Form[Filed Name]"

Thank and good day

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.