Autopostback friendly url

Topics: Developer Forum
Jan 12, 2009 at 10:19 AM
I visit a n2 page with a nice friendly url, on the page I can a calendar which autoposts back when you select a date. Upon autopostback the url changes to the unfriendly actual url that n2 must have been masking.

The same is true for an dropdownlist control with autopostback turned on.

I'm not sure if this is a n2cms issue, or something that needs to be done in all controls that provide autopostback functionality? How is everyone else dealing with this?

I would like the autopostback to postback to the same friendly url.
Jan 12, 2009 at 11:24 AM

"with .NET 3.5 SP1 this issue has been fixed and now, you can programmatically set the form's action tag to the desired friendly URL that you always wanted."


Jan 12, 2009 at 12:06 PM
If your not using 3.5 SP1, ScottGu has an article here

then scroll down to "Handling ASP.NET PostBacks with URL Rewriting"

Is there something like this already in n2, or can it be implemented?
Jan 12, 2009 at 12:32 PM
this seems to work:

FormRewriterControlAdapter.cs file

using System.Web;
using System.Web.UI;

public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
        base.Render(new RewriteFormHtmlTextWriter(writer));

public class RewriteFormHtmlTextWriter : HtmlTextWriter
    public RewriteFormHtmlTextWriter(System.Web.UI.HtmlTextWriter writer)
        : base(writer)
        this.InnerWriter = writer.InnerWriter;

    public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
        : base(writer)
        base.InnerWriter = writer;

    public override void WriteAttribute(string name, string value, bool fEncode)
        // If the attribute we are writing is the "action" attribute, and we are not on a sub-control,
        // then replace the value to write with the raw URL of the request - which ensures that we'll
        // preserve the PathInfo value on postback scenarios

        if (name == "action")
            HttpContext context = HttpContext.Current;
            if (context.Items["ActionAlreadyWritten"] == null)
                // Because we are using the HttpModule, we will use the
                // Request.RawUrl property within ASP.NET to retrieve the origional URL
                // before it was re-written. You'll want to change the line of code below
                // if you use a different URL rewriting implementation.

                value = context.Request.RawUrl;

                // Indicate that we've already rewritten the <form>'s action attribute to prevent
                // us from rewriting a sub-control under the <form> control

                context.Items["ActionAlreadyWritten"] = true;
        base.WriteAttribute(name, value, fEncode);


Form.browser file

  <browser refID="Default">
      <adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
               adapterType="FormRewriterControlAdapter" />

Jan 12, 2009 at 3:01 PM
Or if you have 3.5 SP1.

    protected void Page_PreRender(object sender, EventArgs e)
        this.form1.Action = HttpContext.Current.Request.RawUrl;

in the master page.

Seems to work for the moment, probably going to find a scenario when don't want to change the form action. Can always expose the form from the master page, and have a property on the base page which allows each view to specify if it should rewrite the form action or not.

Feb 28, 2009 at 11:26 AM
Thanks for the tip! Problem solved