using System.Web;

namespace Demo
{
    public class SimpleRewriter : System.Web.IHttpModule
    {

        HttpApplication _application = null;

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new System.EventHandler(context_BeginRequest);
            _application = context;
        }

        public void Dispose()
        {
        }

        private void context_BeginRequest(object sender, System.EventArgs e)
        {
            string requesturl =
                _application.Context.Request.Path.Substring(1,
                    _application.Context.Request.Path.LastIndexOf(@"/")-1
                );

            //Here is where we parse the original request url to determine
            //the querystring parameters for the unfriendly url
            string[] parameters = requesturl.Split(new char[] { '/' });

            if (parameters.Length > 2)
            {
                string firstname = parameters[1];
                string lastname = parameters[2];

                //Rewrite the request path
                _application.Context.RewritePath("~/unfriendly.aspx?firstname=" +
                    firstname + "&lastname=" + lastname);
            }
        }
    }
}