internal MapRouteBuilder Register(string urlTemplate, string method, HandlerFunc handler)
        {
            if (string.IsNullOrWhiteSpace(method))
            {
                throw new ArgumentNullException("method");
            }
            if (string.IsNullOrWhiteSpace(urlTemplate))
            {
                throw new ArgumentNullException("urlTemplate");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            var segments = RouteBuilderHelper.GetUrlTemplateSegments(urlTemplate);
            var route    = new Route
            {
                Method   = method,
                Segments = segments,
                Handler  = handler
            };

            _routes.Add(route);

            return(this);
        }
Exemple #2
0
        private RouteBuilder Register(string method, AppFunc handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            App.Use(async(ctx, next) =>
            {
                if (string.Equals(ctx.Request.Method, method, StringComparison.OrdinalIgnoreCase))
                {
                    var path = ctx.Request.Path.Value.Trim('/');
                    var data = RouteBuilderHelper.MatchData(_urlTemplateSegments, path);
                    if (data != null)
                    {
                        ctx.Set(Keys.RouteData, data);
                        await handler(ctx, next);
                    }
                    else
                    {
                        await next();
                    }
                }
                else
                {
                    await next();
                }
            });

            return(this);
        }
Exemple #3
0
        internal RouteBuilder(IAppBuilder app, string urlTemplate)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            if (string.IsNullOrWhiteSpace(urlTemplate))
            {
                throw new ArgumentNullException("urlTemplate");
            }

            App = app;
            _urlTemplateSegments = RouteBuilderHelper.GetUrlTemplateSegments(urlTemplate);
        }
        internal HandlerFunc GetHandler(IOwinContext ctx)
        {
            foreach (var route in _routes)
            {
                if (!string.Equals(ctx.Request.Method, route.Method, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                var path = ctx.Request.Path.Value.Trim('/');
                var data = RouteBuilderHelper.MatchData(route.Segments, path);
                if (data == null)
                {
                    continue;
                }

                ctx.Set(Keys.RouteData, data);
                return(route.Handler);
            }

            return(null);
        }