Skip to content

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

Updated: at 11:18 PM

 

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, <span class="kwrd">false</span>);
        }

    }</pre>

 

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!