Stop me if you've heard this before:
I'm working through an ASP.NET project and suddenly Postback stops working. I know! This is the simplest of interactions with an ASP.NET web forms application, and it's not WORKING!! What the heck is wrong with this stupid website? I mean, I didn't change anything. I just want my default.aspx form to be able to have a button click button handler. Is that so DIFFICULT? Get your act together FRITZ!1!!
I did some digging and digging on this one. Did I bring in an add-in or library to my project that was preventing the postback from capturing the click event? I broke out Fiddler and analyzed the traffic, to ensure that the form content was indeed being submitted properly back to the server. Everything looked good there.
My next analysis step was to take a look at the Request.Form
collection. I should see the __EVENTARGUMENT
being populated. When I opened the Immediate Window and inspected Request.Form
, this is what I found:
The Visual Studio Immediate Window when I inspected Request.Form
.
How is the Request.Form
collection empty? What's the deal with THAT?
I started thinking about the ASP.NET pipeline, and it hit me: FriendlyUrls
.
There's this interesting thing that happens when web forms attempt to post back to pages managed by FriendlyUrls
and lack a filename. This could be any of the following pages in your site:
You get the idea. These pages for some reason don't handle postback properly. With that in mind, I set forth to drop a quick and dirty change to enable this throughout the website. Fortunately, I can make that change through a URL Rewrite operation. I added the following event handler to my global.asax.cs file:
void Application_BeginRequest(object sender, EventArgs e)
{
var app = (HttpApplication)sender;
if (app.Context.Request.Url.LocalPath.EndsWith("/"))
{
app.Context.RewritePath(
string.Concat(app.Context.Request.Url.LocalPath, "default"));
}
}
With that, my pages started posting back properly... all was right in the world.
Please note: You will have this problem with the default ASP.NET project with web forms when FriendlyUrls
are enabled.