ASP   «Prev  Next»


Lesson 5Request.Form
ObjectiveDescribe how the Request object processes HTML Form data in ASP.NET Core 9.0

How the Request Object processes HTML Form data in ASP.NET Core 9.0.

In ASP.NET Core 9.0, the `HttpRequest` object plays a key role in processing HTML form data submitted via POST or GET methods. When an HTML form is submitted, the `HttpRequest` object (accessed via the `HttpContext.Request`) captures the incoming data and provides various properties and methods to read it.
Here's a breakdown of how the `Request` object processes form data:
✅ 1. Form Submission
When a user submits an HTML form:
<form method="post" action="/submit">
  <input type="text" name="username">
  <input type="password" name="password">
  <button type="submit">Login</button>
</form>

This sends a POST request to the server with data in the request body.
✅ 2. Accessing Form Data via `Request.Form`
ASP.NET Core parses the form body and populates the `Request.Form` collection, which behaves like a dictionary.
Example in a Razor Page or Controller:
public IActionResult Submit()
{
    string username = Request.Form["username"];
    string password = Request.Form["password"];
    // Process the values...
    return View();
}

  • Request.Form is of type IFormCollection
  • It is populated only if the Content-Type is application/x-www-form-urlencoded or multipart/form-data (e.g., for file uploads)

✅ 3. Model Binding Alternative (Preferred)
Instead of manually accessing `Request.Form`, ASP.NET Core can automatically bind form values to parameters or model classes:
[HttpPost]
public IActionResult Submit(LoginModel model)
{
    // model.Username and model.Password are automatically bound
    return View();
}

public class LoginModel
{
    public string Username { get; set; }
    public string Password { get; set; }
}

  • This uses built-in model binding and validation capabilities
  • Request.Form is still used behind the scenes by the model binder


✅ 4. Accessing Query Parameters
For forms submitted with `method="get"`, values are in the URL and accessible via:
string search = Request.Query["search"];

✅ 5. File Uploads via `Request.Form.Files`
For `multipart/form-data`, uploaded files are in `Request.Form.Files`:
var file = Request.Form.Files[0];

🛡 Notes:
  • Form data is only available once, so avoid manually reading the body stream (Request.Body) unless necessary.
  • Request.HasFormContentType can be checked before accessing Request.Form.

✅ Summary
Source Accessor Description
POST Form Request.Form["key"] For form fields with application/x-www-form-urlencoded
File Uploads Request.Form.Files For multipart/form-data
GET Parameters Request.Query["key"] From URL query string
Automatic Binding Controller action parameters Binds form/query data to models or params


Form elements in HTML

Form elements in HTML (text and password input areas, check boxes, radio buttons, and dropdown lists) allow the user to submit a variety of data to the server. But now what? The server needs to read, store, and process this information. The Request.Form version of the Request object can do this.
Reading form data
The following code segment shows part of an HTML input form, specifically a text box containing the user's name:
<INPUT TYPE="TEXT" NAME="Name" SIZE="25" VALUE="Enter your name">

The ASP Form collection allows you to reference this and other data contained in the form. Since the text box form element is named Name, you would reference it as Form("Name"). To write this piece of data back to the browser, you only need to write the ASP code:
<%=Request.Form("Name") %> 

For some form elements (such as <SELECT> or check boxes), you can choose more than one item.
That's not difficult in ASP, because you can specify an index for the Form collection and access one value at a time.
If you don't specify an index, you get a comma-separated list of all the element values. The next lesson describes passing user data in a URL request.

ASP Request Form - Exercise

Click the Exercise link below to use ASP to read and write HTML Form data.
ASP Request Form - Exercise

SEMrush Software