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:
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:
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:
Update the form's action to remove the file extension
-
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 toRedirectMode.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
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.