Using ASP.NET Routing Independent of MVC - passing parameters to the page

Mike Ormond has a nice post about how to use the Asp.Net routing capabilities of Asp.Net MVC, in your standard webforms sites.

In his example Mike goes through the essentials of how to set this up in Application_start and how to create and register your own IRouteHandler.  Mike's example explains this really well with a simple example of routing a single parameter to a matching aspx page as follows:

URL Mapped to:
http://mysite/home http://mysite/RoutingDemoPages/default.aspx
http://mysite/default http://mysite/RoutingDemoPages/default.aspx
http://mysite/page1 http://mysite/RoutingDemoPages/page1.aspx
http://mysite/page2 http://mysite/RoutingDemoPages/page2.aspx

I wanted to see how you can map multiple parameters to a single page and pass that parameter data to the page.  So that the following mappings were made:

URL Mapped to:
http://mysite/fred http://mysite/default.aspx with info on Fred
http://mysite/fred/london http://mysite/default.aspx with info on Fred in London
http://mysite/Mike/Glasgow http://mysite/default.aspx with Mike in Glasgow

 

The first thing to do is to change the routes registered in global.asax, so that we have routes that expect our two parameters for username and area:

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

private void RegisterRoutes(RouteCollection Routes)
{
    Route r = new Route("{username}/{area}" ,new SimpleRouteHandler());
    Route r2 = new Route("{username}", new SimpleRouteHandler());
    Routes.Add(r);
    Routes.Add(r2);
}

 

Next we need to modify the SimpleRouteHandler to deal with these routes.  At first I expected an overload on the CreateInstanceFromVirtualPath method that let me pass some parameters, but that was not available.  Instead I simple added the RouteData (which contains the username and area parameters) to the Context Items collection:

public IHttpHandler GetHttpHandler(RequestContext requestContext)
  {
      string virtualPath = string.Format("~/default.aspx");
      foreach (var value in requestContext.RouteData.Values)
      {
          requestContext.HttpContext.Items[value.Key] = value.Value;
      }
      return (Page)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));

  }

 

Now I can work with those values on default.aspx (and in this case just display them):

protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = string.Format("username:{0} area:{1}", Context.Items["username"], Context.Items["area"]);
}

Cheers

Ian

 

Technorati Tags: ,,

posted @ Monday, May 19, 2008 6:39 PM

Print

Comments on this entry:

# More on ASP.NET Routing

Left by Mike Ormond's Blog at 5/28/2008 10:00 AM
Gravatar
While I've been sunning myself on a Greek Island (Kefalonia - very nice, especially the baclava), Ian

# Culinary art scholorships.

Left by Turkish culinary art e book. at 7/21/2008 9:56 PM
Gravatar
Information on culinary art schools around maine. Culinary art professions. Online culinary art school applications. Turkish culinary art e book.

# hcukcccb

Left by hcukcccb at 7/26/2008 2:42 AM
Gravatar
hcukcccb

# kmehgmpo

Left by kmehgmpo at 9/24/2008 9:11 PM
Gravatar
kmehgmpo

# Risks of snorting percocet.

Left by Risks of snorting percocet. at 12/28/2008 5:26 PM
Gravatar
Risks of snorting percocet.

# Soma.

Left by Soma plant. at 7/8/2009 7:25 AM
Gravatar
Cheap soma. Soma next day delievery. Soma.

# Viagra on line.

Left by Viagra 6 free samples. at 7/10/2009 11:23 PM
Gravatar
Viagra. Viagra price.

# Phentermine.

Left by Phentermine. at 7/11/2009 11:13 PM
Gravatar
Phentermine online. Cheap phentermine. Phentermine. Order phentermine.

# Buy percocet.

Left by Side effects of percocet. at 7/12/2009 11:33 PM
Gravatar
Percocet 93-490. Percocet. Percocet addiction. Percocet addiction .og.

# Order phentermine online.

Left by Phentermine. at 7/13/2009 11:44 PM
Gravatar
Cheap phentermine. Cheap 37 5 phentermine. Phentermine. Buy phentermine cod. Diet tablets phentermine. Abuse phentermine.

# Soma cannabis seeds.

Left by Soma cannabis seeds. at 7/19/2009 6:19 PM
Gravatar
Soma cannabis seeds.

# White tramadol with 377 on the side.

Left by Tramadol. at 7/20/2009 12:25 AM
Gravatar
Tramadol abuse pharmacy discounter. Tramadol shipped to florida. Cheap tramadol. Lowest price tramadol. Tramadol.

# Percocet shipped cod no prior.

Left by Percocet. at 7/21/2009 11:40 PM
Gravatar
Neo percocet. Percocet online. Buy percocet. Buy percocet online. Percocet. Percocet dosages. Percocet shipped cod no prior.

# Viagra.

Left by Buy viagra online. at 7/24/2009 12:05 AM
Gravatar
Viagra. Viagra side effects. Free viagra. Viagra for order lamisil viagra. Buy viagra.

# Taking soma and driving.

Left by Taking soma and driving. at 9/6/2009 5:41 AM
Gravatar
Soma driving. Taking soma and driving. Driving after taking soma.

# Soma bike frame.

Left by Soma bike frame. at 9/6/2009 6:56 PM
Gravatar
Soma bike frame.

# Soma airmail.

Left by Soma airmail. at 9/7/2009 5:45 AM
Gravatar
Soma airmail.

# Soma street price.

Left by Soma street price. at 9/8/2009 6:01 PM
Gravatar
Soma street price.

# Buying soma online without a prescription.

Left by Buying soma online without a prescription. at 9/9/2009 3:26 AM
Gravatar
Buying soma online without a prescription.

# Soma phontos.

Left by Soma phontos. at 9/9/2009 8:35 AM
Gravatar
Soma phontos.
Comments have been closed on this topic.
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910