locked
Url rewriting & postback RRS feed

  • Question

  • User1523315022 posted

    Hi,

    I created a Url Rewriting solution based on some info I found on several post.
    However, when I do a postback on one of the pages (click a submit button for instance) the underlying url is used, and the rewriting fails.

    Any idea how I can solve this ?

    this is in my Application_BeginRequest
     strCurrentPath = Request.Path;
    strCurrentPath = strCurrentPath.ToLower();
    strCustomPath = UrlRewriter.GetURL(strCurrentPath);
    Context.RewritePath(strCustomPath);



    the urlrewriter simply checks the syntax of the url and changes it into the correct on, based on information coming from the database.
    example :
    public static string GetURL(string currentpath)
        {
            string  strCurrentPath = currentpath.ToLower();
            string strCustomPath;
            if (strCurrentPath.IndexOf("/articles/") > -1)
            {
                strCustomPath = "~/Articles.aspx?ID=" + System.IO.Path.GetFileNameWithoutExtension(strCurrentPath);

                // rewrite the URL
                return strCustomPath;
            }


    thanks in advance

    koen

    Thursday, November 17, 2005 5:14 AM

All replies

  • User-672514680 posted
    You'll need to overright the action tag of the form attribute. This can be done in many ways.

    This topic has been exsaulsted in many places, but I'll point you to my favorite article from our very own Scott Mitchell.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/urlrewriting.asp

    Good Luck.
    Thursday, November 17, 2005 5:25 PM
  • User1719664771 posted
    Thanks for the link!  However, using Scott's HtmlForm class, I'm now having problems with the server controls that reside on the page.  The state of the server control doesn't appear to persist on postback.

    For instance:
    <asp:DropDownList id="ddl_Test" AutoPostBack="true" runat="server">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    </asp:DropDownList>

    If I select '2', the postback occurs, but then the dropdownlist gets reset to '1'.  Any suggestions?

    Thanks!
    Friday, December 2, 2005 8:56 PM
  • User-1128085056 posted

    Scott's solution is not complete because it does not render all of the form tag's possible attributes, especially the OnSubmit attribute, which is set via Page.ClientScript.RegisterOnSubmitStatement (in 2.0).   I would suspect that this is causing your problem.

     It is not easy to render in the overridden HtmlForm class because it is accessalble only via internal methods on Page.  There are at least two options for a complete solution:

    1.  Create a overridden HtmlTextWriter that strips out (or better yet replaces) the action attribute.  This writer can be inserter into the process in the RenderChildren method of a overridden Page class.  Since there are at least 2 different HtmlTextWriter implementations, based on the browser's capabilities, the easiest solution I found was to create a proxied writer (via RealProxy).  The performance hit is miniminal since it is only used to write out the Form control's rendering.

    2.  Create a response stream wrapper (set via the Context.Response.Filter property) that parses the output text and replaces the action attribute.  Depending on how you do the parsing, this can be fairly expensive.  One advantage of this appropach is that it can be set inside the URL mapping module, making it transparent to the rest of the app.

    My mapping solution actually uses both of these techniques, the first for most pages, the second as a fallback for any pages that aren't based on the correct class.  Some fairly simple switching logic ensures that the highest performance path normally operates. 

    It works well an anything that I have tried to render- which includes a pretty large range of standard and custom controls, including some AJAX stuff.

    Tuesday, December 6, 2005 9:18 PM
  • User1424575140 posted

    You could try using an IHttpHandlerFactory

     

    http://weblogs.asp.net/andrewseven/articles/UrlRewriting.aspx

    Wednesday, December 7, 2005 2:36 PM
  • User-1699666177 posted

    Have a look here:

    http://odetocode.com/Blogs/scott/archive/2004/09/22/509.aspx

    Seems to be the second RewritePath that you're missing?

    Thursday, December 8, 2005 6:19 AM