PDA

View Full Version : [FIXED] [V0.7] Form action="" Bug



davidhoyt
Nov 12, 2008, 2:32 AM
AjaxEvents don't correctly fire when you have <form action="" /> or the action attribute is missing. In my application we use URL rewriting and to preserve the post url, we wrote a form adapter that takes out the action attribute when rendering form objects. A browser's default behavior when no action attribute is specified is to post back to the url it requested it from.

Here's an example adapter (the one we're using, actually):


using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.Adapters;
using System.IO;

namespace HoytSoft.Common.Web.Adapters {
public class URLRewriteFormAdapter : ControlAdapter {
protected override void Render(System.Web.UI.HtmlTextWriter writer) {
base.Render(new URLRewriteFormAdapterWriter(writer));
}

private class URLRewriteFormAdapterWriter : HtmlTextWriter {
#region Constructors
public URLRewriteFormAdapterWriter(TextWriter writer) : base(writer) {
}

public URLRewriteFormAdapterWriter(TextWriter writer, string tabString) : base(writer, tabString) {
}
#endregion

public override void WriteAttribute(string name, string value, bool fEncode) {
//Basically ignore the "action" attribute...
if ("action".Equals(name, StringComparison.InvariantCultureIgnoreCase))
return;
base.WriteAttribute(name, value, fEncode);
}
}
}
}

In the website, create the App_Browsers folder and create a new browser file. Put this in it:


<!--
Defines a control adapter for rendering an actionless form.
-->
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="HoytSoft.Common.Web.Adapters.URLRewriteFormAdapter" />
</controlAdapters>
</browser>
</browsers>



Then create a page as usual and create an ext button with an ajaxevent click handler. You'll get the error I posted about here: http://forums.ext.net/showthread.php?threadid=3730-4-1.aspx

If you disable actionless forms, it will work normally again.

Thanks for any help you can provide.

davidhoyt
Nov 12, 2008, 3:02 AM
If you absolutely must have a url, I suppose you could use window.location.href - just a thought...

Just thought I should mention, I'm using v0.6.0.

davidhoyt
Nov 12, 2008, 3:59 AM
Attached an example project demonstrating the problem. IE 7 and FF3 both throw an error. If you look at the source for the page you'll notice the form's action attribute is missing.

Thanks for any help you can provide.

davidhoyt
Nov 12, 2008, 2:23 PM
Do you think this fix could make it in before the v0.7 release? We'll be needing it to demo to investors. Thanks guys!

geoffrey.mcgill
Nov 12, 2008, 3:17 PM
Isn't the form's action attribute required? I don't think that page would not validate as valid html. 

If the action is missing, the default dehaviour of the browser might "just happen" to postback to the originating url, although I'm pretty sure that is not part of the html spec.


It should be easy enough to default the Ajax form submit action to window.location.href, but I would consider this more of a "feature addition" instead of "bug fix".

davidhoyt
Nov 12, 2008, 9:03 PM
I think you're correct - it is a required attribute, but I haven't found any documentation on what the behavior should be if it's blank. All the browsers I've come across, however, post back to the current uri. So for at least consistency's sake, it's worth adding.

davidhoyt
Nov 12, 2008, 9:09 PM
This may be helpful: http://www.whatwg.org/specs/web-forms/current-work/#extensions2

In the future, for HTML 5, action will no longer be required. According to that doc: The <CODE>form</CODE> element's <CODE>action</CODE> attribute is no longer a required attribute. Authors may omit it. When the attribute is absent, UAs must act as if the <CODE>action</CODE> attribute was the empty string, which is a relative URI reference, and would thus point to the current document (or the specified base URI, if any).

davidhoyt
Nov 17, 2008, 5:02 PM
Hello --

What's the status of this?

geoffrey.mcgill
Nov 21, 2008, 8:25 AM
Hi davidhoyt,

The logic within AjaxEvents (and AjaxMethod) has been modified to check the following sequence:


1. check for <form>, then use "action" attibute
2. if no form or action, then check to see if .Url property has been set. 
3. if no url, then use window.location.href


One issue we discovered is that IIS will not allow POST requests to a url that ends with a "/". We have not been able to figure out a way around this, although maybe we're just missing something obvious. 


If you run into this POST error, you have two options:


1. Change the url to post to a file name
2. Set Method="GET" property on the AjaxEvent.


Hope this helps.

davidhoyt
Nov 21, 2008, 3:08 PM
Great! Thanks guys!

Regarding the IIS issue - have you tried it with IIS 7 in integrated pipeline mode along with <modules runAllManagedModulesForAllRequests="true" />All requests should then be filtered through ASP.NET with the correct settings.

See http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx for details.

davidhoyt
Dec 18, 2008, 7:16 AM
I just compiled the absolute latest from source and this does not appear to be fixed. My ajax events are not firing when there's no action attribute on my form. If the action attribute is reenabled, it works correctly.

Any thoughts?