Lesson 5 | Request.Form |
Objective | Describe 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
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
