2

Using the following code:

<form id="form1" action="" runat="server">

(even with this code-behind in Page Load: form1.Action = string.Empty;)

...this is the unexpected result that I get:

<form method="post" id="form1" name="form1" action="layouts/Default.aspx">

This is the expected result, that I would like your help to accomplish:

<!-- either action="" or no action attribute at all -->
<form method="post" id="form1" name="form1" action="">

The AppPool is set to run Integrated .NET v2.0.50727 (which I've verified by printing the runtime version). The .NET Framework v4.5 is installed on the server (along with Visual Studio 2012).

This is guaranteed to be some kind of dev environment issue, reason being that the very same codebase outputs action="" for two of my colleagues in their local environments. They largely have the same system environment specifications as I do:

  • Windows Server 2008 R2 64-bit
  • Visual Studio 2012
  • .NET Framework v4.5
  • Same AppPool settings (we've double and triple checked)
  • Same codebase (SVN controlled), including the exact same web.config

This breaking change in ASP.NET v3.5 SP1 (which was better announced in v4.0) could be related. It explains the behaviour. However, it does not explain why only my runtime is getting this breaking change.

Also note that I've tried all available combinations of commands using the aspnet_regiis tool (without avail) i.e. from the different .NET version folders and uninstalling/reinstalling and using -c, -i etc.

Community
  • 1
  • 1
Simeon
  • 5,519
  • 3
  • 29
  • 51

2 Answers2

0

The following ugly workaround is the only solution I've found so far. I'm subclassing the form in order to remove the action attribute on override void RenderBeginTag.

[ToolboxData("<{0}:HtmlFormNoAction runat=\"server\"></{0}:HtmlFormNoAction>")]
public class HtmlFormNoAction : HtmlForm
{
    protected override void RenderBeginTag(HtmlTextWriter writer)
    {
        var htmlString = new StringBuilder(); // this will hold the string
        var stringWriter = new StringWriter(htmlString);
        var htmlWriter = new HtmlTextWriter(stringWriter);
        base.RenderBeginTag(htmlWriter);
        htmlWriter.Flush();
        var formBeginTag = htmlString.ToString();
        var formBeginTagWithoutAction = RemoveActionAttribute(formBeginTag);

        writer.Write(formBeginTagWithoutAction);
    }

    private string RemoveActionAttribute(string formBeginTag)
    {
        const string actionStart = "action=\"";
        if (!formBeginTag.Contains(actionStart))
            return formBeginTag;

        var start = formBeginTag.IndexOf(actionStart);
        var end = formBeginTag.IndexOf("\"", start + actionStart.Length);
        return formBeginTag.Substring(0, start) + formBeginTag.Substring(end + 1);
    }
}
Simeon
  • 5,519
  • 3
  • 29
  • 51
0

check this: https://msdn.microsoft.com/en-us/library/ms972974.aspx

namespace ActionlessForm {
  public class Form : System.Web.UI.HtmlControls.HtmlForm
  {
     protected override void RenderAttributes(HtmlTextWriter writer)
     {
        writer.WriteAttribute("name", this.Name);
        base.Attributes.Remove("name");

        writer.WriteAttribute("method", this.Method);
        base.Attributes.Remove("method");

        this.Attributes.Render(writer);

        base.Attributes.Remove("action");

        if (base.ID != null)
           writer.WriteAttribute("id", base.ClientID);
     }
  }
}

And also this: http://www.codeproject.com/Articles/19672/A-Smart-Form-Control-for-ASP-NET-URL-Rewriting

chendesheng
  • 1,969
  • 18
  • 14