Request.Form Is Empty When Posting To ASPX Page

4.91 (11 votes)

A couple of threads have been posted recently to the ASP.NET forums complaining that values posted to ASPX files are mysteriously disappearing. This post looks at the most likely cause and provides some solutions.

In each reported case, the scenario was very similar - the application in question was an ASP.NET 4.5 Web Forms app, developed in Visual Studio 2013 or 2015. The people reporting the issue were trying to post values using an HTTP POST request to a web form (.aspx file), in one case from an HTML file within the same application, and in the other case, from a browser plugin. The example form provided in one post looked valid enough:

<form action="/Receiver.aspx" method="post">
    <input name="Field1" type="text" value="1" id="Field1" /><br />
    <input name="Field2" type="text" value="some text information here" id="Field2" /><br />
    <input id="Submit1" type="submit" value="submit" />
</form>

But when the form was posted to Receiver.aspx, the values for Request.Form["Field1"] and Request.Form["Field2"] were empty. Even when debugging, the entire Request.Form collection is seen to be empty:

Empty Request.Form

When checking the browser's developer tools Network tab, an interesting fact is revealed - the POST request is successfully completed but immediately followed by a GET request:

Empty Request.Form

This combination of request clears the form data that was posted so that when the second request happens, no data is available,

The culprit is Friendly URLs, which I wrote about in a previous article. Friendly URLs is a framework for managing extensionless URLs in a Web Forms site. It is enabled by default in ASP.NET 4.5 Web Forms sites which are created using the Visual Studio (2013 and 2015) Web Forms application template. The HTML form in this example is designed to post to Receiver.aspx, and the Friendly URLs will take requests that include the file extension and issue an HTTP 301 - Moved Permanently response, directing the browser to make a new (GET) request for the same resource without the file extension.

Solutions

There are three solutions:

  1. Update the form's action to remove the file extension

  2. Amend the configuration for Friendly URLs You will find this in the RouteConfig.cs file in the App_Start folder. The configuration to change is the AutoRedirect mode which by default is set to RedirectMode.Permanent. Comment this setting out:

    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        //settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }

    This will stop requests with file extnesions from being redirected

  3. Disable Friendly URLs

    Comment out the entire body of the RegisterRoutes method in the RouteConfig.cs file.

Summary

This post was made in response to a rash of similar questions being posted to the ASP.NET forums. It explains how Friendly URLs could be one possible reason for empty form collections in an ASP.NET 4.5 Web Forms web site and suggests suitable solutions.

 

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 6690

5 Comments

- satyabrata

Thank you.

- Fredrik

It worked. Thank you!

- federico

thanks!.

- Arkady

Thank you!
I spent a day to understand what problem is before I found your article!
But I made RedirectMode.Off instead of comment.

- flyingtigerfan

Thanks - spent hours trying to figure this out. I'm out of date and trying to start working on some simple asp.net things - and this was absolutely costing me more hair.

Recent Comments

Gfw 03/02/2017 09:48
In response to Free SSL Certificates On IIS With LetsEncrypt
I have used WinSimple for about the last 9 months - works great. One thing that you want to make of...

Ted Driver 02/02/2017 13:24
In response to Free SSL Certificates On IIS With LetsEncrypt
This looks great is you have command line access to your web server - what about those of us on Is...

Carl T. 06/11/2016 05:43
In response to Server.MapPath Equivalent in ASP.NET Core
Very succinct and easy to follow. Worked perfectly the first time for me. Thanks!!...

Manoj Kulkarni 04/11/2016 05:47
In response to Entity Framework Core DbContext Updated
Great post....

Sivu 19/10/2016 08:21
In response to Entity Framework Core TrackGraph For Disconnected Data
Oh that's very very very nice ! Thanks for the write up Mike, much appreciated for the taking the to...

Mark 12/10/2016 16:42
In response to ASP.NET Web Pages vNext or Razor Pages
Although "Web Pages" was removed from the roadmap, has it just been renamed to "Razor Pages"?...

Satyabrata 12/10/2016 09:20
In response to Entity Framework Core TrackGraph For Disconnected Data
Nice article. Please write more articles featuring ASP.Net web pages. Thank you...

Julian 26/09/2016 14:27
In response to Loading ASP.NET Core MVC Views From A Database Or Other Location
Fantastic, many thanks Mike! Had got half way down this road before finding your article - saved...

Abolfazl Roshanzamir 14/09/2016 05:36
In response to Loading ASP.NET Core MVC Views From A Database Or Other Location
Nice article. Thanke you so much ....

cyrus 02/09/2016 15:12
In response to ASP.NET Web Pages vNext or Razor Pages
I've got some news. As Damian stated in this link: https://github.com/aspnet/Mvc/issues/5208 “We...