Can you identify the route name that follows from your web forms page?

I have an ASP.NET 3.5 webforms project with which I have enabled routing. I also have several controls in the project that do different things based on which page they are currently displayed on. It would seem that the easiest way to control this different behavior is to find out which route was used to load the page, and then do something accordingly.

However, I cannot find a way to find the route bar by looking at the actual request URL and running a regular expression over it, which is not very large. Does anyone know how to look for it in another way?

Update: in ASP.NET 4.0 there is still no way to do this. Hope someone else understood this?

+4
source share
3 answers

It seems that Phil Haack answered this question in a blog post on his website: http://haacked.com/archive/2010/11/28/getting-the-route-name-for-a-route.aspx

+3
source

In the .NET 4 webforms application, I used this to define a route definition.

string myOperation = ((System.Web.Routing.Route)(Page.RouteData.Route)).Url; //string has value "Stop" or "Start" 

Let's say your routes are as follows:

  routes.MapPageRoute("StopEmailAlerts", "Stop/{SomeToken}", "~/Emailing.aspx", false); routes.MapPageRoute("SendEmailAlerts", "Start/{SomeToken}", "~/Emailing.aspx", false); 
0
source

I have posted some simple extension methods that you can use to get / set the name of the route in this post. It seems easier (for me) than the Haack solution.

0
source

All Articles