Exemple #1
0
        public void RouteRequestToMethod(RequestContext requestContext)
        {
            HttpRequest request = requestContext.Request;

            foreach (string regex in _routes.Keys)
            {
                RoutesHolder routesHolder = _routes[regex];

                Regex           compiledRegex = routesHolder.Regex;
                MatchCollection matches       = compiledRegex.Matches(request.Url.AbsolutePath);
                if (matches.Count != 0)
                {
                    string method = request.HttpMethod;
                    Action <RequestContext> func;
                    routesHolder.TryGetFunctionForMethod(method, out func);
                    if (func != null)
                    {
                        func.Invoke(requestContext);
                        return;
                    }
                }
            }

            requestContext.Response.StatusCode = 404;
        }
Exemple #2
0
 public void AddRoute(string method, string regex, Action <RequestContext> function)
 {
     if (_routes.ContainsKey(regex))
     {
         _routes[regex].MapMethodToFunction(method, function);
     }
     else
     {
         RoutesHolder routesHolder = new RoutesHolder(regex);
         routesHolder.MapMethodToFunction(method, function);
         _routes.Add(regex, routesHolder);
     }
 }