How To Convert From Request With Parameter to No Parameter in ASP.NET (Server.Transfer)

 

The problem is that I’m trying to keep track of the exact URL a person selects including the request parameter.  That is, I have a URLs that can be played as follows:

http://video.peterkellner.net/TestPage.html?src=P1_Intro.wmv

http://video.peterkellner.net/TestPage.html?src=P2_BasicRIANoTooling.wmv

 

I’ve actually got 7 videos that I want people to be able to play.  The problem is that my web statistics tracker is going to count all videos as coming from TestPage.html and I will not be able to tell which video is getting how much traffic.  What I’d really like is have unique landing pages for each one that I can put links to on my blog, and then have those tracked separately.

So, here is what I would like:

http://video.peterkellner.net/P2_BasicRIANoTooling_wmv

http://video.peterkellner.net/P2_BasicRIANoTooling_wmv

Then, in my web stats, I will see the following

image

To achieve this, the solution is to add a “WildCardMapper” to IIS7 so that all requests, including those without extensions go through the asp.net pipeline.  Then, the requests can be trapped in either a asp.net module, or simply in the global.asax.cs which is how I did it for simplicity.

To do this, the following code needs to be put in the web.config modules section:

    <system.webServer>    
        <modules runAllManagedModulesForAllRequests="true"    >

Then, in the global.asax.cs file, you add the following code:

  protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var incomingUrlFromRequest = HttpContext.Current.Request.Url;
            if (incomingUrlFromRequest != null && incomingUrlFromRequest.Segments.Count() == 2 &&
                incomingUrlFromRequest.Segments[1].EndsWith("_wmv"))
            {
                string newPath = //Context.Request.MapPath("~") +
                    String.Format("TestPage.html?src={0}", incomingUrlFromRequest.Segments[1]).Replace("_wmv", ".wmv");

                Server.Transfer(newPath, false);
            }

        }

 

Now, the translation happens exactly as you would expect.  That is, when you enter the URL: http://video.peterkellner.net/P2_BasicRIANoTooling_wmv invisibly, you are taken to http://video.peterkellner.net/TestPage.html?src=P1_Intro.wmv because Server.Transfer is used.  The browser does not even know the transfer took place, and the URL stays the same in the top of the users browser.

image

Hope this helps!

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP for the past 7 years. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. Hi VVVlad,
    Thanks for he suggestion. I am a native english speaker, I know the rules, but somehow I don’t see the errors. My brain sees it correctly even though it’s wrong. feel free to jump in with any other corrections and I’ll happily make them

  2. I’m not naively English speaker, but I couldn’t read the article and ignore the “video’s”.
    video’s == video is
    videos == plural of video

    sorry for not related comment.

  3. I would recommend using http://urlrewriter.net/ instead of coding it by hand. It’s a free URL rewriter. You can have all of your URL’s setup in an XML file. I think all you need to do is add the config section, and the HTTP Module to the web.config.

    I have even used it in conjunction with a CMS and it worked pretty well. It was a little harder since the CMS had it’s own rewriting going on, but I just had to find the right place to call the URL Rewriter method (instead of using the module).

Follow

Get every new post delivered to your Inbox

Join other followers: