如何通过URL获取RouteData?

我需要在ASP.NET MVC应用程序中通过给定的URL字符串获得
RoutData
。 我找到了基于我的URL字符串模拟
HttpContextBase
的方法,然后将它传递给Route Parsing(Uri to Route)线程中的
RouteTable.Routes.GetRouteData()
方法。 如何使用
RouteTable.Routes.GetRouteData()
模拟
HttpContextBase
通过URL字符串检索
RouteData
? 或者还有另一种通过URL检索
RouteData
的方法吗?     
已邀请:
我使用Moq来确定
HttpContextBase
的成员在ѭ8中使用了什么。他们是: 请求 AppRelativeCurrentExecutionFilePath PATHINFO
Request.AppRelativeCurrentExecutionFilePath
应该返回带有
~
的路径,我需要的是什么,所以实用类可能是这样的:
public static class RouteUtils
{
    public static RouteData GetRouteDataByUrl(string url)
    {
        return RouteTable.Routes.GetRouteData(new RewritedHttpContextBase(url));
    }

    private class RewritedHttpContextBase : HttpContextBase
    {
        private readonly HttpRequestBase mockHttpRequestBase;

        public RewritedHttpContextBase(string appRelativeUrl)
        {
            this.mockHttpRequestBase = new MockHttpRequestBase(appRelativeUrl);
        }


        public override HttpRequestBase Request
        {
            get
            {
                return mockHttpRequestBase;
            }
        }

        private class MockHttpRequestBase : HttpRequestBase
        {
            private readonly string appRelativeUrl;

            public MockHttpRequestBase(string appRelativeUrl)
            {
                this.appRelativeUrl = appRelativeUrl;
            }

            public override string AppRelativeCurrentExecutionFilePath
            {
                get { return appRelativeUrl; }
            }

            public override string PathInfo
            {
                get { return ""; }
            }
        }
    }
}
例如,如果你传递〜/ Error / NotFound,
RouteUtils.GetRouteByUrl("~/Error/NotFound")
会返回类似的内容
RouteData.Values
{
    controller = "Error",
    action = "NotFound"
}
也许这对任何人都有帮助。     
这对我有用(.NET 4.5,MVC 5): https://average-joe.info/url-to-route-data/
System.Web.Routing.RouteData routeFromUrl =
    System.Web.Routing.RouteTable.Routes.GetRouteData(
            new HttpContextWrapper(
                new HttpContext(
                    new HttpRequest(null, path, query),
                    new HttpResponse(new System.IO.StringWriter()))));
    

要回复问题请先登录注册