TOC

The community is working on translating this tutorial into Chinese, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

HttpContext:

Forms (POST data)

In the last article, we discussed the query string and how you can read parameters through it. This will usually happen through a so-called GET request, which will most frequently occur as a result of the user entering a URL in the browser or clicking on a link. As an alternative to the GET request we'll find the POST request. A POST request is usually initiated when the user submits a FORM (like a contact form or a login form). Unlike a GET request, where the data is visible in the URL, there's no visible data for the user in a POST request. For that reason, you need to use another property on the HttpContext class to pick up POST data: The Form property.

In ASP.NET MVC, the most common approach to dealing with forms is to bind the form to a Model. That means that each field of the form is tied to a property on the Model, allowing the form to read from the Model and also write to it, when the form is submitted. This is often referred to as Model Binding, and we'll discuss it elsewhere in this tutorial, but in this article, we'll access FORM data in the old, fashioned way.

Accessing FORM data

The Form property actually works a lot like the Query property - it acts as a Dictionary with keys and their values. So, when you submit a POST request to the Controller, e.g. by submitting a form, all the fields included will be added to the Form dictionary with their name as the Key and their value as the Value. Allow me to illustrate it with a simple example:

<form method="post" action="/Home/FormsTestPost">  
<label for="txtName">Your name:</label>  
<input type="text" id="txtName" name="UserName" />  

<label for="txtAge">Your age:</label>  
<input type="number" id="txtAge" name="UserAge" />  

<button type="submit">Submit</button>  
</form>

Just a basic HTML form. Notice how I set the method attribute to POST - forms support the GET method as well, but it's almost always better to use the POST method when dealing with forms.

My Controller could then look like this:

public class HomeController : Controller  
{  
[HttpGet]  
public IActionResult FormsTest()  
{  
return View();  
}  

[HttpPost]  
public IActionResult FormsTestPost()  
{  
return Content("Hello, " + HttpContext.Request.Form["UserName"] + ". You are " + HttpContext.Request.Form["UserAge"] + " years old!");  
}  
}

Notice how I can access the values of the FORM fields by referencing their names (UserName and UserAge, as defined in the name property of the form fields) in the Form collection.

Summary

Thanks to the HttpContext class and its Form property, accessing FORM/POST data is very easy in ASP.NET MVC. Using it is usually not necessary, thanks to Model Binding, but it's still useful in some cases.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!