Click here to Skip to main content
15,886,788 members
Articles / Web Development / ASP.NET

Pretty URLs, Separation of Layers and O/R Mapping in Web Forms ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.47/5 (14 votes)
12 Sep 200732 min read 86.8K   493   87  
Implementing multi-tier architecture in a web application using ASP.NET 2.0
using System.Web.UI;
using System.Web;




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 : System.Web.UI.HtmlTextWriter
{

    public RewriteFormHtmlTextWriter(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 UrlRewriting.net 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);

    }

}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer iPay, Elizabethtown,Ky
United States United States
After defending his PhD thesis in 2005 (computational nuclear physics) at Vanderbilt in Nashville, the author decided to pursue a career in software development. As a long time open source advocate, he started with writing web applications using Linux-Apache-MySql-P (LAMP) framework. After that experience, he decided to embrace Microsoft technologies.
Currently working as a web developer in .NET platform in the online payments company.

Comments and Discussions