示例#1
0
        public static void Match(this string url, string controller = "Home", string action = "Index", object routeValues = null)
        {
            var httpcontext = new StubHttpContext(requestUrl: url);
            var routes = new RouteCollection();
            MvcApplication.RegisterRoutes(routes);
            RouteData routeData = routes.GetRouteData(httpcontext);

            var _matchRouteValues = true;
            var _routeFailValue = "";
            if (routeValues != null)
            {
                PropertyInfo[] props = routeValues.GetType().GetProperties();

                foreach (var prop in props)
                {
                    if (!prop.GetValue(routeValues, null).Equals(routeData.Values[prop.Name]))
                    {
                        _matchRouteValues = false;
                        _routeFailValue = prop.Name;
                        break;
                    }
                }
            }

            Assert.IsNotNull(routeData, "routeData nulo, url não foi resolvida corretamente");
            Assert.AreEqual(controller, routeData.Values["controller"]);
            Assert.AreEqual(action, routeData.Values["action"]);
            Assert.IsTrue(_matchRouteValues, String.Format("route value '{0}' not match!", _routeFailValue));
        }
示例#2
0
        static UrlHelper UrlHelper(RouteCollection routes = null)
        {
            if (routes == null)
            {
                routes = new RouteCollection();
                MvcApplication.RegisterRoutes(routes);
            }

            StubHttpContext httpContext = new StubHttpContext();
            RequestContext requestContext = new RequestContext(httpContext, new RouteData());
            UrlHelper helper = new UrlHelper(requestContext, routes);
            return helper;
        }