Beispiel #1
0
        public static void Register(HttpConfiguration config)
        {
            SuperscribeConfig.Register(config);
            var engine = RouteEngineFactory.Create();

            config.DependencyResolver = new SuperscribeDependencyAdapter(config.DependencyResolver, engine);

            engine.Route("Any" / Any.Controller / Any.Action / (Int)"siteId");

            var forms     = engine.Route(r => r / "api" / (Long)"parentId" / "Forms".Controller());
            var blog      = engine.Route(r => r / "sites" / (Int)"siteId" / "blog");
            var portfolio = engine.Route(r => r / "sites" / (Int)"siteId" / "portfolio");

            var blogposts = engine.Route(blog / "posts".Controller("blogposts"));

            engine.Route(forms / "VisibleFor" / (String)"appDataId", To.Action("VisibleFor"));
            engine.Route(forms / (Long)"id", To.Action("GetById"));

            engine.Route(blog / "tags".Controller("blogtags"));

            engine.Route(blogposts / (Int)"postId" / "media".Controller("blogpostmedia")
                         / (Int)"id");

            engine.Route(blogposts / "archives".Controller("blogpostarchives") / (Int)"year"
                         / (Int)"month");

            engine.Route(portfolio / "projects".Controller("portfolioprojects")
                         / (Int)"projectId" / "media".Controller("portfolioprojectmedia") / (Int)"id");

            engine.Route(portfolio / "tags".Controller("portfoliotags"));
            engine.Route(portfolio / "categories".Controller("portfoliocategories") / (Int)"id");
        }
Beispiel #2
0
        private static IRouteEngine RegisterCommon(HttpConfiguration configuration, string qualifier, IRouteEngine engine = null)
        {
            if (engine == null)
            {
                engine = RouteEngineFactory.Create();
            }

            configuration.DependencyResolver = new SuperscribeDependencyAdapter(configuration.DependencyResolver, engine);
            configuration.MessageHandlers.Add(new SuperscribeHandler());

            var actionSelector     = configuration.Services.GetService(typeof(IHttpActionSelector)) as IHttpActionSelector;
            var controllerSelector = configuration.Services.GetService(typeof(IHttpControllerSelector)) as IHttpControllerSelector;
            var actionInvoker      = configuration.Services.GetService(typeof(IHttpActionInvoker)) as IHttpActionInvoker;

            configuration.Services.Replace(typeof(IHttpActionSelector), new SuperscribeActionSelectorAdapter(actionSelector));
            configuration.Services.Replace(typeof(IHttpControllerSelector), new SuperscribeControllerSelectorAdapter(controllerSelector));
            configuration.Services.Replace(typeof(IHttpActionInvoker), new SuperscribeActionInvokerAdapter(actionInvoker));

            ControllerTypeCache = new HttpControllerTypeCache(configuration);

            var template = "{*wildcard}";

            if (!string.IsNullOrEmpty(qualifier))
            {
                template = qualifier + "/" + template;
            }

            HttpConfiguration = configuration;

            // We need a single default route that will match everything
            // configuration.Routes.Clear();
            configuration.Routes.MapHttpRoute(
                name: "Superscribe",
                routeTemplate: template,
                defaults: new { });

            return(engine);
        }
Beispiel #3
0
        public IEnumerable <ConfigurationSetupResult> Setup(string applicationEnvironment)
        {
            yield return(new ConfigurationSetupResult("superglue.RoutingImplimentationSetup", environment =>
            {
                var stringRouteParser = new StringRouteParser();

                var define = RouteEngineFactory.Create(new SuperscribeOptions
                {
                    StringRouteParser = stringRouteParser
                });

                environment[SuperscribeEnvironmentExtensions.SuperscribeConstants.Engine] = define;

                environment[RoutingEnvironmentExtensions.RouteConstants.CreateRouteBuilderFunc] = (Func <IRouteBuilder>)(() => new SuperscribeRouteBuilder(environment.ResolveAll <ICheckIfRouteExists>(), stringRouteParser, environment));

                environment[RouteExtensions.RouteConstants.EndpointFromInput] = (Func <object, object>)(environment.GetEndpointFor);

                environment[RouteExtensions.RouteConstants.InputsForEndpoint] = (Func <object, IEnumerable <Type> >)(x =>
                {
                    var route = environment.GetRouteForEndpoint(x);

                    return route == null ? new List <Type>() : route.InputTypes;
                });

                environment[RouteExtensions.RouteConstants.ReverseRoute] = (Func <object, string>)(endpoint =>
                {
                    var endpointRoute = environment.GetRouteForEndpoint(endpoint);

                    if (endpointRoute == null)
                    {
                        return "";
                    }

                    var patternParts = new List <string>();

                    var node = endpointRoute.Node;

                    var appendedParameters = new List <string>();

                    while (node != null)
                    {
                        var paramNode = node as ParamNode;

                        if (paramNode != null)
                        {
                            if (endpointRoute.Parameters.ContainsKey(paramNode.Name))
                            {
                                patternParts.Add(endpointRoute.Parameters[paramNode.Name].ToString());
                                appendedParameters.Add(paramNode.Name);
                            }
                        }
                        else
                        {
                            patternParts.Add(node.Template);
                        }

                        patternParts.Add("/");

                        node = node.Parent;
                    }

                    var missingParameters = endpointRoute.Parameters.Where(x => !IsNullOrDefault(x.Value) && !appendedParameters.Contains(x.Key) && (x.Value.GetType().IsPrimitive || x.Value is string));

                    var patternBuilder = new StringBuilder();
                    patternParts.Reverse();

                    foreach (var part in patternParts)
                    {
                        patternBuilder.Append(part);
                    }

                    var parameterSplitter = "?";

                    foreach (var missingParameter in missingParameters)
                    {
                        patternBuilder.AppendFormat("{0}{1}={2}", parameterSplitter, missingParameter.Key, missingParameter.Value);
                        parameterSplitter = "&";
                    }

                    return patternBuilder.ToString();
                });

                return Task.CompletedTask;
            }, "superglue.RoutingSetup"));
        }