views:

972

answers:

4

In my master pages I have <form ... action="" ...>, in pre SP1, if I viewed the source the action attribute would be an empty string. In SP1 the action attribute is overridden "MyPage.aspx?MyParams", unfortunately, this causes my postbacks to fail as I have additional pathinfo in the URL (ie. MyPage.aspx\CustomerData?MyParams). I have checked the action attribute in the OnLoad event and it is still blank at this time, so somewhere SP1 is overriding this :(.

Sorry, I just realized that part of my post was missing since I did not use the markdown correctly.

+4  A: 

Maybe you can find the solution here in this ASP.NET Forum post (Known Issues / Breaking Changes for ASP.NET in .NET 3.5 Service Pack 1).

Issue

The HtmlForm action attribute is now honored when defined in declarative markup.

Reason

3.5 SP1 added a settable Action property to the HtmlForm type. This new feature makes it much easier for developers to explicitly set the form’s action attribute for scenarios where a developer wants to use a different Url than the normal postback-generated Url. However this change also means that if the action attribute has been set in an .aspx page’s declarative markup, ASP.NET will use the setting from the markup when rendering a <form /> element.

Workaround

Previous versions of ASP.NET always ignored the action attribute if it was present in the declarative markup for a <form /> element. Developers should remove the action attribute from their declarative markup to return to the original behavior where ASP.NET renders the postback Url.

Example

Before (the action attribute was ignored by ASP.NET as dead code):

<form name="form1" method="post" runat="server" action="test.aspx"></form>

3.5 SP1 (remove the action attribute to have ASP.NET render the postback Url):

<form name="form1" method="post" runat="server"></form>
splattne
Actually, it does not seem that ASP.NET is actually honoring when set to an empty string, so this will not work for me.
MrJavaGuy
A: 

What we did in the end was just overode the action user a controladapter. This works for us but is NOT a general solution.

public class HtmlFormAdapter : ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            HtmlForm form = this.Control as HtmlForm;
            if (form == null)
            {
                throw new InvalidOperationException("Can only use HtmlFormAdapter as an adapter for an HtmlForm control");
            }

            base.Render(new CustomActionTextWriter(writer));
        }

        public class CustomActionTextWriter : HtmlTextWriter
        {
            public CustomActionTextWriter(HtmlTextWriter writer) : base(writer)
            {
                this.InnerWriter = writer.InnerWriter;
            }

            public override void WriteAttribute(string name, string value, bool fEncode)
            {
        public override void WriteAttribute(string name, string value, bool fEncode)
        {
 if (name == "action")
 {
  value = "";
 }  
            base.WriteAttribute(name, value, fEncode);  //// override action
        }
    }

}

MrJavaGuy
+1  A: 

Great solution from MrJavaGuy but there is a typo in the code because pasting code in the box here doesn't always work correctly. There is a duplication on the WriteAttribute method, corrected code is as follows -

public class HtmlFormAdapter : ControlAdapter 
{
    protected override void Render(HtmlTextWriter writer)
    {
        HtmlForm form = this.Control as HtmlForm;

        if (form == null)
        {
            throw new InvalidOperationException("Can only use HtmlFormAdapter as an adapter for an HtmlForm control");
        }
        base.Render(new CustomActionTextWriter(writer));
    }


    public class CustomActionTextWriter : HtmlTextWriter
    {
        public CustomActionTextWriter(HtmlTextWriter writer) : base(writer)
        {
            this.InnerWriter = writer.InnerWriter;
        }

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (name == "action")
            {
                value = "";
            }
            base.WriteAttribute(name, value, fEncode);      
        }
    }
}
AndyM
thanks for the correction.
MrJavaGuy
A: 

This might be an old thread but I managed to find an interesting hack. Just set the action of the form to '#' which seems to post back a rewritten url without a problem.

Ariel