Added more examples. WithText and WithJson now specify Utf16 to support rich content.

This commit is contained in:
2022-02-06 21:32:01 -08:00
parent ccad0dd66d
commit 41bbabf1cb
2 changed files with 15 additions and 9 deletions

View File

@ -30,7 +30,8 @@ namespace MontoyaTech.Rest.Net.Example
var listener = new RouteListener(8080,
new Route(HttpRequestMethod.Get, "/status", Status),
new Route<double, double>(HttpRequestMethod.Post, "/add/{a}/{b}", Add),
new Route<User>(HttpRequestMethod.Post, "/signup/{username}", Signup)
new Route<User>(HttpRequestMethod.Post, "/signup/{username}", Signup),
new Route(HttpRequestMethod.Get, "/json", Json)
);
listener.Start();
@ -40,19 +41,24 @@ namespace MontoyaTech.Rest.Net.Example
listener.Block();
}
public static HttpListenerResponse Status(HttpListenerRequest request, HttpListenerResponse response)
public static HttpListenerResponse Status(RouteContext context)
{
return response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational.");
return context.Response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational. 👍");
}
public static HttpListenerResponse Add(HttpListenerRequest request, HttpListenerResponse response, double a, double b)
public static HttpListenerResponse Add(RouteContext context, double a, double b)
{
return response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString());
return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString());
}
public static HttpListenerResponse Signup(HttpListenerRequest request, HttpListenerResponse response, User user)
public static HttpListenerResponse Signup(RouteContext context, User user)
{
return response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name);
return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name);
}
public static HttpListenerResponse Json(RouteContext context)
{
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net"));
}
}
}