, in pre SP1, if I have lo...">

How do I prevent ASP.NET 3.5 SP1 from overriding my action?

In my master pages, I have <form ... action="" ...>

, in pre SP1, if I have looked at the source code, the action attribute will be an empty string. In SP1, the action attribute is overridden by "MyPage.aspx? MyParams" unfortunately this causes my callbacks to fail as I have an additional pathinfo in the url (eg MyPage.aspx \ CustomerData? MyParams). I checked the action attribute in the OnLoad event and at this time is still empty, so somewhere SP1 overrides this :(.

Sorry, I just figured out that part of my post was missing since I misused markdown.

+1


source to share


4 answers


Great solution from MrJavaGuy, but there is a typo in the code because inserting the code into the field does not always work correctly here. There is a duplication of the WriteAttribute method, the corrected code looks like this:



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);      
        }
    }
}

      

+1


source


Perhaps you can find a solution here in this ASP.NET forum post (Known Issues / Breaking Changes for ASP.NET in .NET 3.5 Service Pack 1).

Problem

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

Cause

3.5 SP1 added the Action property to the HtmlForm property. This new feature makes it much easier for developers to explicitly set the form action attribute for scripts when a developer wants to use a different URL than a normal postback-generated URL. However, this change also means that if the action attribute was set in the declarative markup of .aspx pages, ASP.NET will use that setting from the markup when rendering the element <form />

.

Bypass



Previous versions of ASP.NET always ignored the action attribute if it was present in the declarative markup for an element <form />

. Developers must remove the action attribute from their declarative markup to revert to the original behavior when ASP.NET renders the backlink 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 so that ASP.NET will display the backlink URL):

<form name="form1" method="post" runat="server"></form> 

      

+5


source


What we did at the end was just overriding the user action with the managing adapter. This works for us, but 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
        }
    }

      

}

0


source


This may be an old thread, but I managed to find an interesting hack. Just set the form action to "#", which seems to send back the rewritten url without issue.

0


source







All Articles