Skip to content

Commit c09cb72

Browse files
committed
update
1 parent 98d38e0 commit c09cb72

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

WebContext.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Web;
66
using System.Collections.Generic;
77
using System.Collections.Specialized;
8+
using System.Dynamic;
89
using System.Security.Cryptography;
910
using System.Text.Json;
1011

@@ -17,6 +18,7 @@ namespace Maussoft.Mvc
1718
public string Controller;
1819
public string Action;
1920
public string View;
21+
public string RedirectUrl;
2022
public bool Sent;
2123

2224
public Dictionary<string, string> Post;
@@ -37,13 +39,14 @@ public WebContext(HttpListenerContext context, string sessionSavePath)
3739
this.context = context;
3840
this.sessionSavePath = sessionSavePath;
3941

40-
Method = this.context.Request.HttpMethod;
41-
Url = this.context.Request.RawUrl;
42-
Post = new Dictionary<string, string>();
43-
ReadPostData();
42+
this.Method = this.context.Request.HttpMethod;
43+
this.Url = this.context.Request.RawUrl;
44+
this.Post = new Dictionary<string, string>();
45+
this.ReadPostData();
4446

45-
Data = new System.Dynamic.ExpandoObject();
46-
Sent = false;
47+
this.Data = new ViewData();
48+
this.RedirectUrl = null;
49+
this.Sent = false;
4750
}
4851

4952
private String CreateSessionIdentifier()
@@ -157,6 +160,11 @@ FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileSh
157160
return null;
158161
}
159162

163+
public void Redirect(string url)
164+
{
165+
this.RedirectUrl = url;
166+
}
167+
160168
private void ReadPostData()
161169
{
162170
HttpListenerRequest request = this.context.Request;
@@ -174,9 +182,19 @@ private void ReadPostData()
174182

175183
}
176184

177-
internal void SendString(string output, int StatusCode = 200)
185+
internal void SendString(string output, string mimeType = "text/html", int StatusCode = 200)
178186
{
179-
if (!Sent && StatusCode != 200) this.context.Response.StatusCode = StatusCode;
187+
if (Sent)
188+
{
189+
throw new Exception("Output has already been sent");
190+
}
191+
if (RedirectUrl != null)
192+
{
193+
this.context.Response.StatusCode = 302;
194+
this.context.Response.AddHeader("Location", RedirectUrl);
195+
return;
196+
}
197+
if (StatusCode != 200) this.context.Response.StatusCode = StatusCode;
180198
byte[] buf = System.Text.Encoding.UTF8.GetBytes(output);
181199
this.context.Response.ContentLength64 = buf.Length;
182200
this.context.Response.OutputStream.Write(buf, 0, buf.Length);

WebServer.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,23 @@ public void Run()
8787
actionResult = (new ActionRouter<TSession>(this.controllerNamespace)).Route(webctx);
8888
webctx.WriteSession();
8989
if (!actionResult) webctx.View = "Error.NotFound";
90-
viewResult = new ViewRouter<TSession>(this.viewNamespace).Route(webctx);
91-
webctx.FinalizeSession();
92-
if (viewResult == null) webctx.SendString("NotFound", 404);
93-
else webctx.SendString(viewResult);
90+
if (webctx.RedirectUrl == null)
91+
{
92+
viewResult = new ViewRouter<TSession>(this.viewNamespace).Route(webctx);
93+
webctx.FinalizeSession();
94+
if (viewResult == null) webctx.SendString("NotFound", "text/plain", 404);
95+
else webctx.SendString(viewResult);
96+
}
97+
else
98+
{
99+
webctx.SendString(viewResult);
100+
}
94101
}
95102
}
96103
catch (Exception e) // application log
97104
{
98105
Console.WriteLine(e.ToString());
99-
if (webctx != null) webctx.SendString("<pre>" + e.ToString() + "</pre>", 500);
106+
if (webctx != null) webctx.SendString("<pre>" + e.ToString() + "</pre>", "text/html", 500);
100107
}
101108
finally
102109
{

0 commit comments

Comments
 (0)