如何通过URL获取RouteData?

发布于 2024-11-19 17:15:47 字数 465 浏览 5 评论 0原文

我需要通过 ASP.NET MVC 应用程序中给定的 URL 字符串获取 RoutData

我找到了需要根据 URL 字符串模拟 HttpContextBase 的方法,然后将其传递给 路由解析(Uri 到路由) 线程。

如何使用 RouteTable.Routes.GetRouteData() 模拟 HttpContextBase 以通过 URL 字符串检索 RouteData 或者还有其他方法通过 URL 检索 RouteData 吗?

I need to get RoutData by given URL string in ASP.NET MVC application.

I've found the way that I need to mock HttpContextBase based on my URL string and then pass it to RouteTable.Routes.GetRouteData() method in Route Parsing (Uri to Route) thread.

How to mock HttpContextBase to retrieve RouteData by URL string using RouteTable.Routes.GetRouteData()?
Or is there another way to retrieve RouteData by URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め七分饶幸 2024-11-26 17:15:48

我使用 Moq 来确定 HttpContextBase 的哪些成员用于GetRouteData()。它们是:

  • 请求
    • AppRelativeCurrentExecutionFilePath
    • 路径信息

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 ""; }
            }
        }
    }
}

然后,您可以像这样使用它this(例如在 ~/Error/NotFound 上):

var rd = RouteUtils.GetRouteDataByUrl("~/Error/NotFound")

应该返回一个如下所示的对象:

RouteData.Values
{
    controller = "Error",
    action = "NotFound"
}

I used Moq to determine what members of HttpContextBase are used in GetRouteData(). They are:

  • Request
    • AppRelativeCurrentExecutionFilePath
    • PathInfo

Request.AppRelativeCurrentExecutionFilePath should return path with ~, what I exactly need, so utility class may be like this one:

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 ""; }
            }
        }
    }
}

Then, you can use it like this (for example on ~/Error/NotFound):

var rd = RouteUtils.GetRouteDataByUrl("~/Error/NotFound")

Which should return an object that looks like this:

RouteData.Values
{
    controller = "Error",
    action = "NotFound"
}
别想她 2024-11-26 17:15:48

这对我有用(.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()))));

This works for me (.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()))));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文