String URL to RouteValueDictionary

前端 未结 4 696
盖世英雄少女心
盖世英雄少女心 2020-11-27 18:02

Is there as easy way to convert string URL to RouteValueDictionary collection? Some method like UrlToRouteValueDictionary(string url).

I need such metho

相关标签:
4条回答
  • 2020-11-27 18:31

    You would need to create a mocked HttpContext as routes constrains requires it.

    Here is an example that I use to unit test my routes (it was copied from Pro ASP.Net MVC framework):

            RouteCollection routeConfig = new RouteCollection();
            MvcApplication.RegisterRoutes(routeConfig);
            var mockHttpContext = new MockedHttpContext(url);
            RouteData routeData = routeConfig.GetRouteData(mockHttpContext.Object);
            // routeData.Values is an instance of RouteValueDictionary
            //...
    
    0 讨论(0)
  • 2020-11-27 18:43

    I wouldn't rely on RouteTable.Routes.GetRouteData from previous examples because in that case you risk missing some values (for example if your query string parameters don't fully fit any of registered mapping routes). Also, my version doesn't require mocking a bunch of request/response/context objects.

    public static RouteValueDictionary UrlToRouteValueDictionary(string url)
    {
        int queryPos = url.IndexOf('?');
    
        if (queryPos != -1)
        {
            string queryString = url.Substring(queryPos + 1);
            var valuesCollection = HttpUtility.ParseQueryString(queryString);
            return new RouteValueDictionary(valuesCollection.AllKeys.ToDictionary(k => k, k => (object)valuesCollection[k]));
        }
    
        return new RouteValueDictionary();
    }
    
    0 讨论(0)
  • 2020-11-27 18:50

    Here is a solution that doesn't require mocking:

    var request = new HttpRequest(null, "http://localhost:3333/Home/About", "testvalue=1");
    var response = new HttpResponse(new StringWriter());
    var httpContext = new HttpContext(request, response);
    var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
    var values = routeData.Values;
    // The following should be true for initial version of mvc app.
    values["controller"] == "Home"
    values["action"] == "Index"
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 18:51

    Here is a solution that doesn't require instantiating a bunch of new classes.

    var httpContext = context.Get<System.Web.HttpContextWrapper>("System.Web.HttpContextBase");
    var routeData = System.Web.Routing.RouteTable.Routes.GetRouteData(httpContext);
    var values = routeData.Values;
    var controller = values["controller"];
    var action = values["action"];
    

    The owin context contains an environment that includes the HttpContext.

    0 讨论(0)
提交回复
热议问题