Added more examples. WithText and WithJson now specify Utf16 to support rich content.
This commit is contained in:
parent
ccad0dd66d
commit
41bbabf1cb
@ -30,7 +30,8 @@ namespace MontoyaTech.Rest.Net.Example
|
|||||||
var listener = new RouteListener(8080,
|
var listener = new RouteListener(8080,
|
||||||
new Route(HttpRequestMethod.Get, "/status", Status),
|
new Route(HttpRequestMethod.Get, "/status", Status),
|
||||||
new Route<double, double>(HttpRequestMethod.Post, "/add/{a}/{b}", Add),
|
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();
|
listener.Start();
|
||||||
@ -40,19 +41,24 @@ namespace MontoyaTech.Rest.Net.Example
|
|||||||
listener.Block();
|
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"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static HttpListenerResponse WithText(this HttpListenerResponse response, string text)
|
public static HttpListenerResponse WithText(this HttpListenerResponse response, string text)
|
||||||
{
|
{
|
||||||
response.ContentType = "text/plain";
|
response.ContentType = "text/plain; charset=utf-16";
|
||||||
|
|
||||||
var bytes = Encoding.Unicode.GetBytes(text);
|
var bytes = Encoding.Unicode.GetBytes(text);
|
||||||
response.OutputStream.Write(bytes, 0, bytes.Length);
|
response.OutputStream.Write(bytes, 0, bytes.Length);
|
||||||
@ -38,7 +38,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static HttpListenerResponse WithJson(this HttpListenerResponse response, object obj)
|
public static HttpListenerResponse WithJson(this HttpListenerResponse response, object obj)
|
||||||
{
|
{
|
||||||
response.ContentType = "application/json";
|
response.ContentType = "application/json; charset=utf-16";
|
||||||
|
|
||||||
var bytes = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(obj));
|
var bytes = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(obj));
|
||||||
response.OutputStream.Write(bytes, 0, bytes.Length);
|
response.OutputStream.Write(bytes, 0, bytes.Length);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user