Added new route exception event handler. Renamed events. Added better example code. Bumped nuget version to 1.1.2.

This commit is contained in:
2023-01-26 10:08:56 -08:00
parent 77f13ac7ae
commit b4414f4da2
9 changed files with 112 additions and 73 deletions

View File

@ -34,29 +34,44 @@ namespace MontoyaTech.Rest.Net.Example
new Route(HttpRequestMethod.Get, "/json", Json)
);
listener.RequestPreProcessEvent += (ListenerContext context) => {
Console.WriteLine("Request start: " + context.Request.RawUrl);
return true;
};
listener.RequestPostProcessEvent += (ListenerContext context) =>
{
Console.WriteLine("Request end: " + context.Request.RawUrl);
};
listener.Start();
Console.WriteLine("Available routes:");
foreach (var route in listener.Routes)
Console.WriteLine($"- [{route.Method}] {route.Syntax}");
Console.WriteLine($"Rest api server running at http://localhost:{listener.Port}");
listener.Block();
}
public static HttpListenerResponse Status(RouteContext context)
public static HttpListenerResponse Status(ListenerContext context)
{
return context.Response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational. 👍");
}
public static HttpListenerResponse Add(RouteContext context, double a, double b)
public static HttpListenerResponse Add(ListenerContext context, double a, double b)
{
return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString());
}
public static HttpListenerResponse Signup(RouteContext context, User user)
public static HttpListenerResponse Signup(ListenerContext context, User user)
{
return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name);
}
public static HttpListenerResponse Json(RouteContext context)
public static HttpListenerResponse Json(ListenerContext context)
{
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net"));
}