I have a ASP.NET 3.5 webforms project I have enabled routing over. I also have in the project a number of controls doing different things based on what page they are currently being shown in. It would seem that the most straightforward way to control this different behavior is to discover which route was used to load the page and then do things according to that.
我有一个ASP.NET 3.5 webforms项目,我启用了路由。我还在项目中有许多控件根据当前显示的页面执行不同的操作。看起来控制这种不同行为的最直接的方法是发现用于加载页面的路径然后按照那样做。
However, I can't seem to find a way to discover the route bar looking at the actual request URL and running a regex over it which isn't great. Does anyone know a way to look it up some other way?
但是,我似乎无法找到一种方法来发现路由栏查看实际请求URL并对其运行正则表达式并不是很好。有没有人知道以其他方式查找它的方法?
Update: there still doesn't appear to be a way to do this in ASP.NET 4.0. Hopefully someone else has figured this out?
更新:在ASP.NET 4.0中似乎仍然没有办法实现此目的。希望其他人已经想出来了吗?
3 个解决方案
#1
3
It looks like Phil Haack has answered this question in a blog post on his site: http://haacked.com/archive/2010/11/28/getting-the-route-name-for-a-route.aspx
看起来Phil Haack在他的网站上的博客文章中回答了这个问题:http://haacked.com/archive/2010/11/28/getting-the-route-name-for-a-route.aspx
#2
0
In a .NET 4 webforms app, I used this to determine the route definition.
在.NET 4 webforms应用程序中,我使用它来确定路由定义。
string myOperation =
((System.Web.Routing.Route)(Page.RouteData.Route)).Url;
//string has value "Stop" or "Start"
Let's say your routes are like so:
假设你的路线是这样的:
routes.MapPageRoute("StopEmailAlerts",
"Stop/{SomeToken}",
"~/Emailing.aspx", false);
routes.MapPageRoute("SendEmailAlerts",
"Start/{SomeToken}",
"~/Emailing.aspx", false);
#3
0
I posted a couple of simple extension methods you can use to get/set the route name on this post. Seems simpler (to me) than Haack's solution.
我发布了一些简单的扩展方法,您可以使用它们来获取/设置此帖子上的路由名称。似乎比哈克的解决方案更简单(对我而言)。
#1
3
It looks like Phil Haack has answered this question in a blog post on his site: http://haacked.com/archive/2010/11/28/getting-the-route-name-for-a-route.aspx
看起来Phil Haack在他的网站上的博客文章中回答了这个问题:http://haacked.com/archive/2010/11/28/getting-the-route-name-for-a-route.aspx
#2
0
In a .NET 4 webforms app, I used this to determine the route definition.
在.NET 4 webforms应用程序中,我使用它来确定路由定义。
string myOperation =
((System.Web.Routing.Route)(Page.RouteData.Route)).Url;
//string has value "Stop" or "Start"
Let's say your routes are like so:
假设你的路线是这样的:
routes.MapPageRoute("StopEmailAlerts",
"Stop/{SomeToken}",
"~/Emailing.aspx", false);
routes.MapPageRoute("SendEmailAlerts",
"Start/{SomeToken}",
"~/Emailing.aspx", false);
#3
0
I posted a couple of simple extension methods you can use to get/set the route name on this post. Seems simpler (to me) than Haack's solution.
我发布了一些简单的扩展方法,您可以使用它们来获取/设置此帖子上的路由名称。似乎比哈克的解决方案更简单(对我而言)。