Beispiel #1
0
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            if (!context.TargetIsAction)
            {
                throw new InvalidOperationException("AutoNamedRouteAttribute should be only used on actions");
            }
            if (!context.Actions.Any())
            {
                throw new InvalidOperationException("Actions should be empty");
            }

            IDirectRouteBuilder builder = context.CreateBuilder(Template);
            // generate name:
            // get the first action descriptor from our list because we only need to get its name
            var actionDescriptor     = context.Actions.First();
            var controllerDescriptor = actionDescriptor.ControllerDescriptor;
            var actionName           = actionDescriptor.ActionName;
            var controllerName       = controllerDescriptor.ControllerName;
            // get area also
            var areaName = getAreaName(controllerDescriptor);

            // compose a name
            builder.Name  = T4Extensions.ComposeAutoRouteName(areaName, controllerName, actionName);
            builder.Order = Order;
            return(builder.Build());
        }
        public void CreateRoute_UsesOrderPropertyWhenBuilding()
        {
            // Arrange
            int expectedOrder             = 123;
            RouteFactoryAttribute product = CreateProductUnderTest();

            product.Order = expectedOrder;

            int order = 0;
            IDirectRouteBuilder builder = null;

            builder = CreateBuilder(
                () =>
            {
                order = builder.Order;
                return(null);
            }
                );
            DirectRouteFactoryContext context = CreateContext((i) => builder);

            // Act
            RouteEntry ignore = product.CreateRoute(context);

            // Assert
            Assert.Equal(expectedOrder, order);
        }
        public void CreateRoute_UsesNamePropertyWhenBuilding()
        {
            // Arrange
            string expectedName           = "RouteName";
            RouteFactoryAttribute product = CreateProductUnderTest();

            product.Name = expectedName;

            string name = null;
            IDirectRouteBuilder builder = null;

            builder = CreateBuilder(
                () =>
            {
                name = builder.Name;
                return(null);
            }
                );
            DirectRouteFactoryContext context = CreateContext((i) => builder);

            // Act
            RouteEntry ignore = product.CreateRoute(context);

            // Assert
            Assert.Same(expectedName, name);
        }
Beispiel #4
0
        private static IHttpRoute BuildWithResolver(
            string template,
            IInlineConstraintResolver constraintResolver
            )
        {
            HttpActionDescriptor[] actions = new HttpActionDescriptor[]
            {
                new ReflectedHttpActionDescriptor()
            };
            DirectRouteFactoryContext context = new DirectRouteFactoryContext(
                null,
                actions,
                constraintResolver,
                targetIsAction: true
                );

            // Act
            IDirectRouteBuilder builder = context.CreateBuilder(template);
            IHttpRoute          route   = builder.Build().Route;

            // Assertions for default, unspecified behavior:
            Assert.NotNull(route);
            Assert.Equal(actions, route.DataTokens["actions"]);

            return(route);
        }
Beispiel #5
0
        private static RouteEntry CreateRouteEntry(
            string prefix,
            IDirectRouteFactory factory,
            IReadOnlyCollection <HttpActionDescriptor> actions,
            IInlineConstraintResolver constraintResolver,
            bool targetIsAction
            )
        {
            Contract.Assert(factory != null);

            DirectRouteFactoryContext context = new DirectRouteFactoryContext(
                prefix,
                actions,
                constraintResolver,
                targetIsAction
                );
            RouteEntry entry = factory.CreateRoute(context);

            if (entry == null)
            {
                throw Error.InvalidOperation(
                          SRResources.TypeMethodMustNotReturnNull,
                          typeof(IDirectRouteFactory).Name,
                          "CreateRoute"
                          );
            }

            DirectRouteBuilder.ValidateRouteEntry(entry);

            return(entry);
        }
        public void CreateRoute_IfBuilderConstraintsIsNull_UsesConstraintsPropertyWhenBuilding()
        {
            // Arrange
            TRouteDictionary             expectedConstraints = new TRouteDictionaryConcrete();
            Mock <RouteFactoryAttribute> productMock         = CreateProductUnderTestMock();

            productMock.SetupGet(p => p.Constraints).Returns(expectedConstraints);
            IDirectRouteFactory product = productMock.Object;

            RouteEntry expectedEntry = CreateEntry();

            TRouteDictionary    constraints = null;
            IDirectRouteBuilder builder     = null;

            builder = CreateBuilder(() =>
            {
                constraints = builder.Constraints;
                return(null);
            });
            Assert.Null(builder.Constraints); // Guard
            DirectRouteFactoryContext context = CreateContext((i) => builder);

            // Act
            RouteEntry ignore = product.CreateRoute(context);

            // Assert
            Assert.Same(expectedConstraints, constraints);
        }
        public void CreateRoute_IfBuilderDataTokensIsNotNullAndDataTokensPropertyIsNull_UsesBuilderDataTokens()
        {
            // Arrange
            TRouteDictionary existingDataTokens = new TRouteDictionaryConcrete();

            Mock <RouteFactoryAttribute> productMock = CreateProductUnderTestMock();

            productMock.SetupGet(p => p.DataTokens).Returns((TRouteDictionary)null);
            IDirectRouteFactory product = productMock.Object;

            RouteEntry expectedEntry = CreateEntry();

            TRouteDictionary    dataTokens = null;
            IDirectRouteBuilder builder    = null;

            builder = CreateBuilder(() =>
            {
                dataTokens = builder.DataTokens;
                return(null);
            });

            builder.DataTokens = existingDataTokens;

            DirectRouteFactoryContext context = CreateContext((i) => builder);

            // Act
            RouteEntry ignore = product.CreateRoute(context);

            // Assert
            Assert.Same(existingDataTokens, dataTokens);
        }
        public HttpRouteFactory(string prefix = "api")
        {
            var constraintResolver = new DefaultInlineConstraintResolver();
            List <HttpActionDescriptor> actionDescriptors = new List <HttpActionDescriptor>();

            _routeFactoryContext = new DirectRouteFactoryContext(prefix, actionDescriptors, constraintResolver, false);
        }
        public C1PathInfoRouteConstraint(String suffix)
        {
            var routeFactory = new DirectRouteFactoryContext(String.Empty, String.Empty, new List <ActionDescriptor>(), new DefaultInlineConstraintResolver(), true);
            var builder      = routeFactory.CreateBuilder(suffix);
            var route        = new Route(builder.Template, builder.Defaults, builder.Constraints, null);

            _routeInfo = new RouteInfo(route);
        }
 private static IHttpRoute BuildWithoutResolver(string template,
     IReadOnlyCollection<HttpActionDescriptor> actions)
 {
     DirectRouteFactoryContext context = new DirectRouteFactoryContext(null, actions,
         new Mock<IInlineConstraintResolver>(MockBehavior.Strict).Object, targetIsAction: true);
     IDirectRouteBuilder builder = context.CreateBuilder(template, constraintResolver: null);
     return builder.Build().Route;
 }
Beispiel #11
0
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            IDirectRouteBuilder builder = context.CreateBuilder(Template);

            builder.Name  = Name;
            builder.Order = Order;
            return(builder.Build());
        }
        private static IHttpRoute BuildWithoutResolver(string template,
                                                       IReadOnlyCollection <HttpActionDescriptor> actions)
        {
            DirectRouteFactoryContext context = new DirectRouteFactoryContext(null, actions,
                                                                              new Mock <IInlineConstraintResolver>(MockBehavior.Strict).Object, targetIsAction: true);
            IDirectRouteBuilder builder = context.CreateBuilder(template, constraintResolver: null);

            return(builder.Build().Route);
        }
        public void CreateRoute_IfContextIsNull_Throws()
        {
            // Arrange
            DirectRouteFactoryContext context = null;
            IDirectRouteFactory       product = CreateProductUnderTest();

            // Act & Assert
            Assert.ThrowsArgumentNull(() => product.CreateRoute(context), "context");
        }
        public void CreateRoute_IfBuilderDataTokensIsNotNull_UpdatesDataTokensFromPropertyWhenBuilding()
        {
            // Arrange
            TRouteDictionary existingDataTokens     = new TRouteDictionaryConcrete();
            string           existingDataTokenKey   = "ExistingDataTokenKey";
            object           existingDataTokenValue = "ExistingDataToken";

            existingDataTokens.Add(existingDataTokenKey, existingDataTokenValue);
            string conflictingDataTokenKey      = "ConflictingDataTokenKey";
            object oldConflictingDataTokenValue = "OldConflictingDataToken";

            existingDataTokens.Add(conflictingDataTokenKey, oldConflictingDataTokenValue);

            TRouteDictionary additionalDataTokens     = new TRouteDictionaryConcrete();
            string           additionalDataTokenKey   = "NewDataTokenKey";
            string           additionalDataTokenValue = "NewDataToken";

            additionalDataTokens.Add(additionalDataTokenKey, additionalDataTokenValue);
            string newConflictingDataTokenValue = "NewConflictingDataToken";

            additionalDataTokens.Add(conflictingDataTokenKey, newConflictingDataTokenValue);

            Mock <RouteFactoryAttribute> productMock = CreateProductUnderTestMock();

            productMock.SetupGet(p => p.DataTokens).Returns(additionalDataTokens);
            IDirectRouteFactory product = productMock.Object;

            RouteEntry expectedEntry = CreateEntry();

            TRouteDictionary    dataTokens = null;
            IDirectRouteBuilder builder    = null;

            builder = CreateBuilder(
                () =>
            {
                dataTokens = builder.DataTokens;
                return(null);
            }
                );

            builder.DataTokens = existingDataTokens;

            DirectRouteFactoryContext context = CreateContext((i) => builder);

            // Act
            RouteEntry ignore = product.CreateRoute(context);

            // Assert
            Assert.Same(existingDataTokens, dataTokens);
            Assert.Equal(3, dataTokens.Count);
            Assert.True(dataTokens.ContainsKey(existingDataTokenKey));
            Assert.Same(existingDataTokenValue, dataTokens[existingDataTokenKey]);
            Assert.True(dataTokens.ContainsKey(conflictingDataTokenKey));
            Assert.Same(newConflictingDataTokenValue, dataTokens[conflictingDataTokenKey]);
            Assert.True(dataTokens.ContainsKey(additionalDataTokenKey));
            Assert.Same(additionalDataTokenValue, dataTokens[additionalDataTokenKey]);
        }
Beispiel #15
0
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            var result = ((IDirectRouteFactory)InnerAttribute).CreateRoute(context);

            var writeableResult = new RouteEntry(result.Name, new WriteableRoute(result.Route));

            //need to add this here so we can retrieve it later
            writeableResult.Route.DataTokens.Add("Umb_RouteName", Name);
            return(writeableResult);
        }
        /// <summary>
        /// Creates a direct route entry.
        /// </summary>
        /// <param name="context">The context to use to create the route.</param>
        /// <returns>
        /// The direct route entry.
        /// </returns>
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            // context.Actions will always contain at least one action - and all of the
            // actions will always belong to the same controller.
            IDirectRouteBuilder builder = context.CreateBuilder(this.Template);

            builder.Name  = this.Name;
            builder.Order = this.Order;
            return(builder.Build());
        }
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            var builder = context.CreateBuilder(Template);

            builder.Name = Name;
            builder.Order = Order;
            builder.DataTokens = new Dictionary<string, object>();
            builder.DataTokens["culture"] = Culture ?? "en-US";
            return builder.Build();
        }
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            var builder = context.CreateBuilder(Template);

            builder.Name                  = Name;
            builder.Order                 = Order;
            builder.DataTokens            = new Dictionary <string, object>();
            builder.DataTokens["culture"] = Culture ?? "en-US";
            return(builder.Build());
        }
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            var result = ((IDirectRouteFactory)InnerAttribute).CreateRoute(context);

            var writeableResult = new RouteEntry(result.Name, new WriteableRoute(result.Route));

            //need to add this here so we can retrieve it later
            writeableResult.Route.DataTokens.Add("Umb_RouteName", Name);
            return writeableResult;
        }
Beispiel #20
0
        public void BuildRouteTemplate(
            string areaPrefix,
            string prefix,
            string template,
            string expected
            )
        {
            var result = DirectRouteFactoryContext.BuildRouteTemplate(areaPrefix, prefix, template);

            Assert.Equal(expected, result);
        }
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            var builder = context.CreateBuilder(Template);

            builder.Name  = Name;
            builder.Order = Order;
            var result = builder.Build();

            return(result);
            //return new RouteEntry(Template, new HttpRoute(Template));
        }
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            Contract.Assert(context != null);

            IDirectRouteBuilder builder = context.CreateBuilder(Template);
            Contract.Assert(builder != null);

            builder.Name = Name;
            builder.Order = Order;
            return builder.Build();
        }
Beispiel #23
0
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            Contract.Assert(context != null);

            IDirectRouteBuilder builder = context.CreateBuilder(Template);

            Contract.Assert(builder != null);

            builder.Name  = Name;
            builder.Order = Order;
            return(builder.Build());
        }
 public new RouteEntry CreateRoute(DirectRouteFactoryContext context)
 {
     // context.Actions will always contain at least one action - and all of the
     // actions will always belong to the same controller.
     var controllerDescriptor = context.Actions.First().ControllerDescriptor;
     var template = Template.Replace("{controller}",
         controllerDescriptor.ControllerName);
     IDirectRouteBuilder builder = context.CreateBuilder(template);
     builder.Name = controllerDescriptor.ControllerName+"Api";
     builder.Order = Order;
     return builder.Build();
 }
Beispiel #25
0
 public RouteEntry CreateRoute(DirectRouteFactoryContext context)
 {
     var builder = context.CreateBuilder(string.Empty);
     var action = builder.Actions.First();
     var attr = action.GetCustomAttributes<System.Web.Http.RouteAttribute>(true).FirstOrDefault();
     if(attr != null)
     {
         builder.Template = string.Concat(builder.Template, "/", attr.Template);
     }
     var res = context.CreateBuilder(string.Empty).Build();
     return res;
 }
Beispiel #26
0
    public new RouteEntry CreateRoute(DirectRouteFactoryContext context)
    {
        // context.Actions will always contain at least one action - and all of the
        // actions will always belong to the same controller.
        var controllerDescriptor = context.Actions.First().ControllerDescriptor;
        var template             = Template.Replace("{controller}",
                                                    controllerDescriptor.ControllerName);
        IDirectRouteBuilder builder = context.CreateBuilder(template);

        builder.Name  = Name;
        builder.Order = Order;
        return(builder.Build());
    }
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            var action = context.Actions.FirstOrDefault();
            var template = string.Format("{0}/{1}", action.ControllerDescriptor.ControllerName, action.ActionName);
            if (!string.IsNullOrWhiteSpace(Template))
            {
                template += "/" + Template;
            }
            var builder = context.CreateBuilder(template);

            builder.Name = Name;
            builder.Order = Order;
            return builder.Build();
        }
        /// <summary>
        /// Creates <see cref="T:System.Web.Http.Routing.RouteEntry" /> instances based on the provided factories and action. The route entries provide direct routing to the provided action.
        /// </summary>
        /// <returns>A set of route entries.</returns>
        /// <param name="actionDescriptor">The action descriptor.</param>
        /// <param name="factories">The direct route factories.</param>
        /// <param name="constraintResolver">The constraint resolver.</param>
        protected override IReadOnlyList <RouteEntry> GetActionDirectRoutes(HttpActionDescriptor actionDescriptor,
                                                                            IReadOnlyList <IDirectRouteFactory> factories, IInlineConstraintResolver constraintResolver)
        {
            Contract.Assume(actionDescriptor != null);

            // let the DefaultDirectRouteProvider create the normal routes based on all factories
            var routes =
                new List <RouteEntry>(base.GetActionDirectRoutes(actionDescriptor, factories, constraintResolver));

            // check if the current action is part of the default API version and the action is not version neutral
            var defaultVersion        = actionDescriptor.Configuration.GetApiVersioningOptions().DefaultApiVersion;
            var versionModel          = actionDescriptor.ControllerDescriptor.GetApiVersionModel();
            var isDefaultVersionRoute = !versionModel.IsApiVersionNeutral && versionModel.DeclaredApiVersions.Any(v => v == defaultVersion);

            if (isDefaultVersionRoute)
            {
                // find all routes for the action that have a version constraint
                var versionizedRoutes = routes.Where(r => r.Route.Constraints.Any(c => c.Value is ApiVersionRouteConstraint)).ToArray();
                foreach (var versionizedRoute in versionizedRoutes)
                {
                    // separate the URL template into the path segments
                    var chunks = versionizedRoute.Route.RouteTemplate.Split('/');

                    // build the {version} string based on the route constraint name
                    var versionConstraint = "{" +
                                            versionizedRoute.Route.Constraints.Where(
                        c => c.Value is ApiVersionRouteConstraint).Select(c => c.Key).First() +
                                            "}";

                    // join the segments again together but remove the chunks containing the URL constraint
                    var newRouteTemplate = string.Join("/", chunks.Where(c => !c.Contains(versionConstraint)));

                    // build a new route for the new URL template
                    var context = new DirectRouteFactoryContext(string.Empty, new[] { actionDescriptor },
                                                                constraintResolver, false);
                    var builder = context.CreateBuilder(newRouteTemplate);
                    if (!string.IsNullOrEmpty(versionizedRoute.Name))
                    {
                        builder.Name = versionizedRoute.Name + "DefaultVersion";
                    }
                    builder.DataTokens = new Dictionary <string, object>
                    {
                        { IsDefaultVersionRouteKey, true }
                    };
                    routes.Add(builder.Build());
                }
            }

            return(routes);
        }
Beispiel #29
0
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            Contract.Assert(context != null);

            IDirectRouteBuilder builder = context.CreateBuilder(Template);

            Contract.Assert(builder != null);

            builder.Name  = Name;
            builder.Order = Order;
            var entry = builder.Build();

            return(new LocalizedRouteEntry(entry.Name, entry.Route, ExplicitCulture, TranslateUrl));
        }
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            Contract.Assert(context != null);

            IDirectRouteBuilder builder = context.CreateBuilder(_infoProvider.Template);
            Contract.Assert(builder != null);

            builder.Name = _infoProvider.Name;

#if ASPNETWEBAPI
            builder.Order = _infoProvider.Order;
#endif

            return builder.Build();
        }
Beispiel #31
0
		RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
		{
			Contract.Assert(context != null);

			if (context.InlineConstraintResolver is RavenInlineConstraintResolver == false)
				return FakeRouteEntry;

			IDirectRouteBuilder builder = context.CreateBuilder(Template);
			Contract.Assert(builder != null);

			builder.Name = Name;
			builder.Order = Order;

			return builder.Build();
		}
        private static IHttpRoute BuildWithResolver(string template, IInlineConstraintResolver constraintResolver)
        {
            HttpActionDescriptor[] actions = new HttpActionDescriptor[] { new ReflectedHttpActionDescriptor() };
            DirectRouteFactoryContext context = new DirectRouteFactoryContext(null, actions, constraintResolver, targetIsAction: true);

            // Act
            IDirectRouteBuilder builder = context.CreateBuilder(template);
            IHttpRoute route = builder.Build().Route;

            // Assertions for default, unspecified behavior:
            Assert.NotNull(route);
            Assert.Equal(actions, route.DataTokens["actions"]);

            return route;
        }
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            var action   = context.Actions.FirstOrDefault();
            var template = string.Format("{0}/{1}", action.ControllerDescriptor.ControllerName, action.ActionName);

            if (!string.IsNullOrWhiteSpace(Template))
            {
                template += "/" + Template;
            }
            var builder = context.CreateBuilder(template);

            builder.Name  = Name;
            builder.Order = Order;
            return(builder.Build());
        }
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            Contract.Assert(context != null);

            IDirectRouteBuilder builder = context.CreateBuilder(_infoProvider.Template);

            Contract.Assert(builder != null);

            builder.Name = _infoProvider.Name;

#if ASPNETWEBAPI
            builder.Order = _infoProvider.Order;
#endif

            return(builder.Build());
        }
Beispiel #35
0
        /// <summary>
        /// Route Factory
        /// </summary>
        /// <param name="context">Route Context</param>
        /// <returns></returns>
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            string controllerName = context.Actions.FirstOrDefault().ControllerDescriptor.ControllerName;

            //Format: {apiVersion}/{controller}/{template}
            string template = String.Format(
                "{0}{1}{2}{3}",
                (String.IsNullOrEmpty(Gale.REST.Config.GaleConfig.apiVersion) ? "" : Gale.REST.Config.GaleConfig.apiVersion + "/"),
                controllerName,
                (_template.StartsWith("/") ? "" : "/"),
                _template
                );

            return((new System.Web.Http.RouteAttribute(template)
            {
            } as IDirectRouteFactory).CreateRoute(context));
        }
Beispiel #36
0
        protected RouteEntry createRouteInternal(DirectRouteFactoryContext context)
        {
            string actionName     = context.Actions.FirstOrDefault().ActionName;
            string controllerName = context.Actions.FirstOrDefault().ControllerDescriptor.ControllerName;
            string route          = $"{controllerName}/{actionName}";

            if (DirectRoutingEnabled && RouteEncryptionService.EncryptionUsed())
            {
                route = RouteEncryptionService.GetEncrypted(route);
            }

            IDirectRouteBuilder builder = context.CreateBuilder(route);

            //builder.Name = this.Name;
            //builder.Order = this.Order;
            return(builder.Build());
        }
Beispiel #37
0
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            var tmplt = "api" + "/" + Template;

            var assembly = context.Actions.FirstOrDefault().ControllerDescriptor.ControllerType.Assembly;
            var plugin   = PluginLoader.Instance[assembly];

            if (plugin == null)
            {
                throw new Exception("has to have a matching plugin");
            }

            IDirectRouteBuilder builder = context.CreateBuilder("modules/" + plugin.Name + "/" + tmplt);

            builder.Name  = Name;
            builder.Order = Order;
            return(builder.Build());
        }
        public void CreateRoute_DelegatesToContextCreateBuilderBuild()
        {
            // Arrange
            string expectedTemplate     = "RouteTemplate";
            IDirectRouteFactory product = CreateProductUnderTest(expectedTemplate);

            RouteEntry expectedEntry = CreateEntry();

            IDirectRouteBuilder       builder = CreateBuilder(() => expectedEntry);
            DirectRouteFactoryContext context = CreateContext((template) => template == expectedTemplate ? builder :
                                                              new DirectRouteBuilder(new TActionDescriptor[0], targetIsAction: true));

            // Act
            RouteEntry entry = product.CreateRoute(context);

            // Assert
            Assert.Same(expectedEntry, entry);
        }
        /// <summary>
        /// Route Factory
        /// </summary>
        /// <param name="context">Route Context</param>
        /// <returns></returns>
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            string controllerName = context.Actions.FirstOrDefault().ControllerDescriptor.ControllerName;

            //Format: {apiVersion}/{controller}/{template}
            string template = String.Format(
                "{0}/{1}{2}{3}",
                   Gale.REST.Config.GaleConfig.apiVersion,
                   controllerName,
                   (_template.StartsWith("/") ? "" : "/"),
                   _template
               );

            return (new System.Web.Http.RouteAttribute(template)
            {

            } as IDirectRouteFactory).CreateRoute(context);
        }
Beispiel #40
0
        RouteEntry IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
        {
            Contract.Assert(context != null);

            if (context.InlineConstraintResolver is RavenInlineConstraintResolver == false)
            {
                return(FakeRouteEntry);
            }

            IDirectRouteBuilder builder = context.CreateBuilder(Template);

            Contract.Assert(builder != null);

            builder.Name  = Name;
            builder.Order = Order;

            return(builder.Build());
        }
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            // Current action method
            var actionDescriptor = context.Actions.First();

            var currentVersion = GetVersion(actionDescriptor);

            IDirectRouteBuilder builder = context.CreateBuilder(Template);

            builder.Constraints.Add(
                new KeyValuePair <string, object>("version", new ApiVersionConstraint(currentVersion)));
            builder.Name  = Name;
            builder.Order = Order;

            return(builder.Build());
        }
        /// <inheritdoc />
        public RouteEntry CreateRoute(DirectRouteFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            IDirectRouteBuilder builder = context.CreateBuilder(Template);
            Contract.Assert(builder != null);
            builder.Name = Name;
            builder.Order = Order;

            TRouteDictionary builderDefaults = builder.Defaults;

            if (builderDefaults == null)
            {
                builder.Defaults = Defaults;
            }
            else
            {
                TRouteDictionary defaults = Defaults;

                if (defaults != null)
                {
                    foreach (KeyValuePair<string, object> defaultItem in defaults)
                    {
                        builderDefaults[defaultItem.Key] = defaultItem.Value;
                    }
                }
            }

            TRouteDictionary builderConstraints = builder.Constraints;

            if (builderConstraints == null)
            {
                builder.Constraints = Constraints;
            }
            else
            {
                TRouteDictionary constraints = Constraints;

                if (constraints != null)
                {
                    foreach (KeyValuePair<string, object> constraint in constraints)
                    {
                        builderConstraints[constraint.Key] = constraint.Value;
                    }
                }
            }

            TRouteDictionary builderDataTokens = builder.DataTokens;

            if (builderDataTokens == null)
            {
                builder.DataTokens = DataTokens;
            }
            else
            {
                TRouteDictionary dataTokens = DataTokens;

                if (dataTokens != null)
                {
                    foreach (KeyValuePair<string, object> dataToken in dataTokens)
                    {
                        builderDataTokens[dataToken.Key] = dataToken.Value;
                    }
                }
            }

            return builder.Build();
        }
        internal static RouteEntry CreateRouteEntry(
            string prefix, 
            IDirectRouteFactory factory,
            IReadOnlyCollection<HttpActionDescriptor> actions, 
            IInlineConstraintResolver constraintResolver,
            bool targetIsAction)
        {
            Contract.Assert(factory != null);

            DirectRouteFactoryContext context = new DirectRouteFactoryContext(prefix, actions, constraintResolver, targetIsAction);
            RouteEntry entry = factory.CreateRoute(context);

            if (entry == null)
            {
                throw Error.InvalidOperation(SRResources.TypeMethodMustNotReturnNull,
                    typeof(IDirectRouteFactory).Name, "CreateRoute");
            }

            IHttpRoute route = entry.Route;
            Contract.Assert(route != null);

            HttpActionDescriptor[] targetActions = GetTargetActionDescriptors(route);

            if (targetActions == null || targetActions.Length == 0)
            {
                throw new InvalidOperationException(SRResources.DirectRoute_MissingActionDescriptors);
            }

            if (route.Handler != null)
            {
                throw new InvalidOperationException(SRResources.DirectRoute_HandlerNotSupported);
            }

            return entry;
        }
        private static RouteEntry CreateRouteEntry(
            string prefix,
            IDirectRouteFactory factory,
            IReadOnlyCollection<HttpActionDescriptor> actions,
            IInlineConstraintResolver constraintResolver,
            bool targetIsAction)
        {
            Contract.Assert(factory != null);

            DirectRouteFactoryContext context = new DirectRouteFactoryContext(prefix, actions, constraintResolver, targetIsAction);
            RouteEntry entry = factory.CreateRoute(context);

            if (entry == null)
            {
                throw Error.InvalidOperation(SRResources.TypeMethodMustNotReturnNull,
                    typeof(IDirectRouteFactory).Name, "CreateRoute");
            }

            DirectRouteBuilder.ValidateRouteEntry(entry);

            return entry;
        }
 public HttpRouteFactory(string prefix = "api")
 {
     var constraintResolver = new DefaultInlineConstraintResolver();
     List<HttpActionDescriptor> actionDescriptors = new List<HttpActionDescriptor>();
     _routeFactoryContext = new DirectRouteFactoryContext(prefix, actionDescriptors, constraintResolver, false);
 }