private void HandleRequest(HttpListenerContext ctx, List <string> el, Dictionary <string, string> urlParams) { string url = "/"; IRequest request = new Request(ctx, DataProvider, AuthenticationProvider, urlParams); string[] splitUrl = ctx.Request.Url?.LocalPath.TrimStart('/').TrimEnd('/').Split("/"); for (int i = 0; i < el.Count; i++) { if (el[i].StartsWith(":")) { url += $":{el[i].TrimStart(':')}/"; } else { url += $"{splitUrl[i]}/"; } } IApiController controller = Controllers[url.TrimEnd('/')]; switch (ctx.Request.HttpMethod.ToUpper()) { case "GET": { controller.Get(request); break; } case "POST": { controller.Post(request); break; } case "PUT": { controller.Put(request); break; } case "PATCH": { controller.Patch(request); break; } case "DELETE": { controller.Delete(request); break; } } }
private async void HandleIndex(HttpListenerContext ctx) { string url = "/"; IRequest request = new Request(ctx, DataProvider, AuthenticationProvider, new Dictionary <string, string>() { }); if (!Controllers.ContainsKey("/")) { HttpListenerResponse resp = ctx.Response; byte[] data = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(new { Error = "404, Index Not Found", Status = 404 })); resp.StatusCode = 404; resp.ContentType = "application/json"; resp.ContentEncoding = Encoding.UTF8; resp.ContentLength64 = data.LongLength; await resp.OutputStream.WriteAsync(data, 0, data.Length); resp.Close(); return; } IApiController controller = Controllers["/"]; switch (ctx.Request.HttpMethod.ToUpper()) { case "GET": { controller.Get(request); break; } case "POST": { controller.Post(request); break; } case "PUT": { controller.Put(request); break; } case "PATCH": { controller.Patch(request); break; } case "DELETE": { controller.Delete(request); break; } } }