测试请求的URL是否在路由表中 [英] Test if a request's URL is in the Route table
本文介绍了测试请求的URL是否在路由表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想测试URL是否是Global.asax
中定义的路由的一部分.这就是我所拥有的:
I want to test if a URL is part of the routes defined in the Global.asax
. This is what I have:
var TheRequest = HttpContext.Current.Request.Url.AbsolutePath.ToString();
var TheRoutes = System.Web.Routing.RouteTable.Routes;
foreach (var TheRoute in TheRoutes)
{
if (TheRequest == TheRoute.Url) //problem here
{
RequestIsInRoutes = true;
}
}
问题是我无法从路线中提取网址.我需要更改什么?
The problem is that I can’t extract the URL from the route. What do I need to change?
推荐答案
这就是我最终要做的事情:
This is what I ended up doing:
string TheRequest = HttpContext.Current.Request.Url.AbsolutePath.ToString();
foreach (Route r in System.Web.Routing.RouteTable.Routes)
{
if (("/" + r.Url) == TheRequest)
{
//the request is in the routes
}
}
它很笨拙,但可以在3行中工作.
It's hacky but it works in 3 lines.
这篇关于测试请求的URL是否在路由表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文