diff --git a/Rest.Net/Extensions/HttpListenerResponseExtensions.cs b/Rest.Net/Extensions/HttpListenerResponseExtensions.cs index aa9dcc8..da50c7c 100644 --- a/Rest.Net/Extensions/HttpListenerResponseExtensions.cs +++ b/Rest.Net/Extensions/HttpListenerResponseExtensions.cs @@ -65,6 +65,22 @@ namespace MontoyaTech.Rest.Net return response; } + /// + /// Sets the response content type to html and writes the given html to it. + /// + /// + /// + /// This response. + public static HttpListenerResponse WithHtml(this HttpListenerResponse response, string html) + { + response.ContentType = "text/html; charset=utf-16"; + + var bytes = Encoding.Unicode.GetBytes(html); + response.OutputStream.Write(bytes, 0, bytes.Length); + + return response; + } + /// /// Sets the status code for a given response. /// @@ -187,5 +203,27 @@ namespace MontoyaTech.Rest.Net return response; } + + /// + /// Sets a redirect for a given response. + /// + /// + /// + /// This response. + public static HttpListenerResponse WithRedirect(this HttpListenerResponse response, string url) + { + try + { + response.StatusCode = (int)HttpStatusCode.Redirect; + + response.AddHeader("Location", url); + + return response; + } + catch + { + return response; + } + } } } diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index 4805ea3..3e554d8 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,8 +17,8 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.1.0 - Upgraded project to DotNet 6 and added an exception catch around the main listener thread to prevent crashes where the entire Reset service goes down. + 1.1.1 + HttpListener now returns NotFound if no route was found. Added WithRedirect and WithHtml extensions. diff --git a/Rest.Net/RouteListener.cs b/Rest.Net/RouteListener.cs index d060a77..0c50ba2 100644 --- a/Rest.Net/RouteListener.cs +++ b/Rest.Net/RouteListener.cs @@ -156,7 +156,7 @@ namespace MontoyaTech.Rest.Net catch { } if (!handled) - ctx.Response.WithStatus(HttpStatusCode.BadRequest); + ctx.Response.WithStatus(HttpStatusCode.NotFound); if (close) ctx.Response.Close();