Request.Form Is Empty When Posting To ASPX Page

4.92 (12 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: 8707

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

Pam 30/08/2017 11:30
In response to Sending Email in Razor Pages
Mike, RazorPages sound like a nice choice for somebody still working in ASP classic who wants to to...

Robby Robson 15/08/2017 00:43
In response to Routing in Razor Pages
Mike: great stuff. Now that .Core Standard 2.0 is formally out, how soon will you rewrite your book...

Satyabrata Mohapatra 28/07/2017 08:59
In response to Sending Email in Razor Pages
Bit off topic, but congratulation sir for your MVP award. You deserve it !!!...

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using asp.net web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...