Free ASP.NET Hosting – :: How to Read Form Values

With new HttpContext in ASP.NET MVC 6 you will also get new request class. One markable change is how form variables are read from request. As there is no key-value collection with form variables that is filled by default when request is created, you have to read form values asynchronously. Here’s the example how to do it.

FREE banner HostforlifeAs far as I understand then the decision to not load form variables by default is related to keeping HttpContext object graph minimal. New request object has asynchronous method called GetFormAsync() and we will use this method to load form variables.

One thing to notice – items in collection have type KeyValuePair<string, string[]>, so there can be more than one value for each form variable. This why I call FirstOrDefault() extension method on form values (yes, it’s not absolutely correct but this is just example). To find out more take a look at default implementation of GetFormAsync() method.

Wrapping Up

HttpContext in next version of ASP.NET MVC is very small. Reading form variables is good example of how HttpContext object graph is held small. Variables are read on-demand basis and if your application doesn’t give MVC any reason to go for form variables then by default form variables are not touched. Free ASP.NET Hosting

Try their Free ASP.NET Hosting today and your account will be setup soon! You can also take advantage of their Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc.

hostforlife banner

Leave a Reply

Your email address will not be published.