Наследование: System.Web.Routing.RouteBase
Пример #1
0
		public void ShouldNotBeNullIfRouteIsMatched()
		{
			var route = new RegexRoute("asdf", handler);
			RouteData routeData = route.GetRouteData("asdf");
			Assert.AreSame(handler, routeData.RouteHandler);
			Assert.IsNotNull(routeData, "routeData should not be null if the path matched the regex");
		}
Пример #2
0
		public void ShouldHaveMatchedPairIfValueFound()
		{
			var route = new RegexRoute(@"(?<Controller>[a-zA-Z]+)_((?<Action>[a-zA-Z]+)_)?(?<Id>\d+)?", handler);
			RouteData routeData = route.GetRouteData("Products_View_0");
			Assert.AreSame(handler, routeData.RouteHandler);
			Assert.AreEqual("Products", routeData.Values["Controller"]);
			Assert.AreEqual("View", routeData.Values["Action"]);
			Assert.AreEqual("0", routeData.Values["Id"]);
		}
Пример #3
0
		public void ShouldHaveDefaultValuesIfPairFoundButDoesNotMatchAnything()
		{
			var defaults = new RouteValueDictionary(new {a = "A"});
			var route = new RegexRoute("asdf", defaults, handler);
			RouteData routeData = route.GetRouteData("asdf");
			Assert.AreSame(handler, routeData.RouteHandler);
			Assert.IsNotNull(routeData, "routeData should not be null if the path matched the regex");
			Assert.IsTrue(routeData.Values.ContainsKey("a"));
			Assert.AreEqual("A", routeData.Values["a"], "if the match failed, the value should be default");
		}
Пример #4
0
 public void RouteGeneratorCallsCustomFunctionIfProvided()
 {
     bool called = false;
     RequestContext innerContext = null;
     RouteValueDictionary innerRouteValues = null;
     RegexRoute innerRoute = null;
     VirtualPathData innerPathData = null;
     var route = new RegexRoute(
         @"(?<Controller>[a-zA-Z]+)(_(?<Action>[a-zA-Z]+))?(_?<Id>\d+)?",
         delegate(RequestContext context, RouteValueDictionary routeValues, RegexRoute thisRoute)
             {
                 called = true;
                 innerContext = context;
                 innerRouteValues = routeValues;
                 innerRoute = thisRoute;
                 innerPathData = new VirtualPathData(thisRoute, "");
                 return innerPathData;
             },
         handler);
     RouteData routeData = route.GetRouteData("Products_View_0");
     var values = new RouteValueDictionary(new {Controller = "Accounts", Action = "Delete", Id = 0});
     var requestContext = new RequestContext(mr.PartialMock<HttpContextBase>(), routeData);
     VirtualPathData pathData = route.GetVirtualPath(
         requestContext,
         values);
     Assert.IsTrue(called);
     Assert.IsNotNull(innerContext);
     Assert.AreSame(requestContext, innerContext);
     Assert.IsNotNull(innerRouteValues);
     Assert.AreSame(values, innerRouteValues);
     Assert.IsNotNull(innerRoute);
     Assert.AreSame(route, innerRoute);
     Assert.IsNotNull(pathData);
     Assert.AreSame(pathData, innerPathData);
 }
Пример #5
0
 public void ShouldReturnNullIfRouteIsNotMatched()
 {
     var route = new RegexRoute("asdf", handler);
     RouteData routeData = route.GetRouteData("fdsa");
     Assert.IsNull(routeData, "routedata should be null if the path didn't match");
 }
Пример #6
0
 public void RouteGeneratorReturnsNullWhenNotAllParametersFilledIn()
 {
     var route = new RegexRoute(@"(?<Controller>[a-zA-Z]+)(_(?<Action>[a-zA-Z]+))?(_?<Id>\d+)?",
                                "{Controller}_{Action}_{Id}", handler);
     RouteData routeData = route.GetRouteData("Products_View_0");
     VirtualPathData pathData = route.GetVirtualPath(new RequestContext(mr.PartialMock<HttpContextBase>(), routeData),
                                                     new RouteValueDictionary(
                                                         new {Controller = "Accounts", Action = "Delete"}));
     Assert.IsNull(pathData);
 }
Пример #7
0
 public void RouteGeneratorGeneratesValidRouteWithoutDefaults()
 {
     var route = new RegexRoute(@"(?<Controller>[a-zA-Z]+)(_(?<Action>[a-zA-Z]+))?(_?<Id>\d+)?",
                                "{Controller}_{Action}_{Id}", handler);
     RouteData routeData = route.GetRouteData("Products_View_0");
     VirtualPathData pathData = route.GetVirtualPath(new RequestContext(mr.PartialMock<HttpContextBase>(), routeData),
                                                     new RouteValueDictionary(
                                                         new {Controller = "Accounts", Action = "Delete", Id = 0}));
     Assert.AreEqual("Accounts_Delete_0", pathData.VirtualPath);
 }
Пример #8
0
        private static VirtualPathData RealGetVirtualPath(RequestContext requestContext, RouteValueDictionary values, RegexRoute thisRoute)
        {
            var pathDictionary = new Dictionary<string, string>();
            if (thisRoute.Defaults != null)
            {
                foreach (var pair in thisRoute.Defaults)
                {
                    pathDictionary.Add(pair.Key, pair.Value.ToString());
                }
            }
            if (values != null)
            {
                foreach (var pair in values)
                {
                    pathDictionary[pair.Key] = pair.Value.ToString();
                }
            }
            string newUrl = thisRoute.UrlGenerator;
            foreach (var pair in pathDictionary)
            {
                newUrl = newUrl.Replace("{" + pair.Key + "}", pair.Value);
            }

            if (Regex.IsMatch(newUrl, @"\{\w+\}"))
            {
                return null;
            }
            return new VirtualPathData(thisRoute, newUrl);
        }
        private static VirtualPathData RealGetVirtualPath(RequestContext requestContext, RouteValueDictionary values, RegexRoute thisRoute)
        {
            var pathDictionary = new Dictionary <string, string>();

            if (thisRoute.Defaults != null)
            {
                foreach (var pair in thisRoute.Defaults)
                {
                    pathDictionary.Add(pair.Key, pair.Value.ToString());
                }
            }
            if (values != null)
            {
                foreach (var pair in values)
                {
                    pathDictionary[pair.Key] = pair.Value.ToString();
                }
            }
            string newUrl = thisRoute.UrlGenerator;

            foreach (var pair in pathDictionary)
            {
                newUrl = newUrl.Replace("{" + pair.Key + "}", pair.Value);
            }

            if (Regex.IsMatch(newUrl, @"\{\w+\}"))
            {
                return(null);
            }
            return(new VirtualPathData(thisRoute, newUrl));
        }