diff --git a/WebContext.cs b/WebContext.cs index 9ff0edf..3b2841b 100644 --- a/WebContext.cs +++ b/WebContext.cs @@ -5,6 +5,7 @@ using System.Web; using System.Collections.Generic; using System.Collections.Specialized; +using System.Dynamic; using System.Security.Cryptography; using System.Text.Json; @@ -17,6 +18,7 @@ namespace Maussoft.Mvc public string Controller; public string Action; public string View; + public string RedirectUrl; public bool Sent; public Dictionary Post; @@ -37,13 +39,14 @@ public WebContext(HttpListenerContext context, string sessionSavePath) this.context = context; this.sessionSavePath = sessionSavePath; - Method = this.context.Request.HttpMethod; - Url = this.context.Request.RawUrl; - Post = new Dictionary(); - ReadPostData(); + this.Method = this.context.Request.HttpMethod; + this.Url = this.context.Request.RawUrl; + this.Post = new Dictionary(); + this.ReadPostData(); - Data = new System.Dynamic.ExpandoObject(); - Sent = false; + this.Data = new ViewData(); + this.RedirectUrl = null; + this.Sent = false; } private String CreateSessionIdentifier() @@ -157,6 +160,11 @@ FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileSh return null; } + public void Redirect(string url) + { + this.RedirectUrl = url; + } + private void ReadPostData() { HttpListenerRequest request = this.context.Request; @@ -174,9 +182,19 @@ private void ReadPostData() } - internal void SendString(string output, int StatusCode = 200) + internal void SendString(string output, string mimeType = "text/html", int StatusCode = 200) { - if (!Sent && StatusCode != 200) this.context.Response.StatusCode = StatusCode; + if (Sent) + { + throw new Exception("Output has already been sent"); + } + if (RedirectUrl != null) + { + this.context.Response.StatusCode = 302; + this.context.Response.AddHeader("Location", RedirectUrl); + return; + } + if (StatusCode != 200) this.context.Response.StatusCode = StatusCode; byte[] buf = System.Text.Encoding.UTF8.GetBytes(output); this.context.Response.ContentLength64 = buf.Length; this.context.Response.OutputStream.Write(buf, 0, buf.Length); diff --git a/WebServer.cs b/WebServer.cs index 5382626..289c6d1 100644 --- a/WebServer.cs +++ b/WebServer.cs @@ -87,16 +87,23 @@ public void Run() actionResult = (new ActionRouter(this.controllerNamespace)).Route(webctx); webctx.WriteSession(); if (!actionResult) webctx.View = "Error.NotFound"; - viewResult = new ViewRouter(this.viewNamespace).Route(webctx); - webctx.FinalizeSession(); - if (viewResult == null) webctx.SendString("NotFound", 404); - else webctx.SendString(viewResult); + if (webctx.RedirectUrl == null) + { + viewResult = new ViewRouter(this.viewNamespace).Route(webctx); + webctx.FinalizeSession(); + if (viewResult == null) webctx.SendString("NotFound", "text/plain", 404); + else webctx.SendString(viewResult); + } + else + { + webctx.SendString(viewResult); + } } } catch (Exception e) // application log { Console.WriteLine(e.ToString()); - if (webctx != null) webctx.SendString("
" + e.ToString() + "
", 500); + if (webctx != null) webctx.SendString("
" + e.ToString() + "
", "text/html", 500); } finally {