private UrlHelper CreateUrlHelper(string requestPath)
        {
            string expectedUrl = "{controller}/{action}/{id}";
            RouteValueDictionary expectedDefaults = new RouteValueDictionary()
            {
                { RouteConstants.controller, "Home" },
                { RouteConstants.action, "Index" },
                { RouteConstants.id, UrlParameter.Optional },
            };

            string[] expectedNamespaces = new[] { "Some.Namespace" };

            Assembly assembly = Assembly.GetExecutingAssembly();

            string applicationDefaultNamespace = "Some";

            var routeCollection = new RouteCollection();

            var area = new SomeAreaRegistration();

            area.RegisterArea(new AreaRegistrationContext(area.AreaName, routeCollection));

            Route route = routeCollection.RegisterDefaultRoutes(expectedUrl, expectedDefaults, expectedNamespaces, assembly, applicationDefaultNamespace);

            var testHttpContext = new TestHttpContext(new WebTestContext()
            {
                AppRelativeCurrentExecutionFilePath = requestPath,
            });

            var routeData = routeCollection.GetRouteData(testHttpContext);

            var requestContext = new RequestContext(testHttpContext, routeData);

            return(new UrlHelper(requestContext, routeCollection));
        }
Example #2
0
        public void Should_register_default_routes_with_areas_registered_first()
        {
            var area = new SomeAreaRegistration();

            area.RegisterArea(new AreaRegistrationContext(area.AreaName, sut));

            Route route = sut.RegisterDefaultRoutes(expectedUrl, expectedDefaults, expectedNamespaces, assembly, applicationDefaultNamespace);

            Assert.Equal(6, sut.Count);

            var defaultAreaRoute = sut[AppAreaRegistration.GetAreaDefaultRouteName(area.AreaName)] as Route;

            Assert.NotNull(defaultAreaRoute);

            Assert.Equal($"{area.AreaName}/{expectedUrl}", defaultAreaRoute.Url);

            AssertRouteDefaults(defaultAreaRoute, RouteConstants.controller, expectedDefaults[RouteConstants.controller]);
            AssertRouteDefaults(defaultAreaRoute, RouteConstants.action, expectedDefaults[RouteConstants.action]);
            AssertRouteDefaults(defaultAreaRoute, RouteConstants.id, expectedDefaults[RouteConstants.id]);

            object areaScriptResourceRouteName  = GetAreaResourceRouteName(area.AreaName, DefaultFolders.Scripts);
            object areaContentResourceRouteName = GetAreaResourceRouteName(area.AreaName, DefaultFolders.Content);

            AssertDataToken(defaultAreaRoute, AppAreaRegistration.DataTokens.areaFolder, area.AreaFolder);
            AssertDataToken(defaultAreaRoute, AppAreaRegistration.DataTokens.areaDefaultRouteName, AppAreaRegistration.GetAreaDefaultRouteName(area.AreaName));

            AssertDataToken(defaultAreaRoute, DataTokens.areaScriptResourceRouteName, areaScriptResourceRouteName);
            AssertDataToken(defaultAreaRoute, DataTokens.areaContentResourceRouteName, areaContentResourceRouteName);

            AssertDataToken(defaultAreaRoute, DataTokens.defaultScriptResourceRouteName, defaultScriptResourceRouteName);
            AssertDataToken(defaultAreaRoute, DataTokens.defaultContentResourceRouteName, defaultContentResourceRouteName);

            AssertRouteExcludeConstraint(defaultAreaRoute);

            AssertResourceRoute(defaultScriptResourceRouteName);
            AssertResourceRoute(defaultContentResourceRouteName);
        }