5
5
using System . Web ;
6
6
using System . Collections . Generic ;
7
7
using System . Collections . Specialized ;
8
+ using System . Dynamic ;
8
9
using System . Security . Cryptography ;
9
10
using System . Text . Json ;
10
11
@@ -17,6 +18,7 @@ namespace Maussoft.Mvc
17
18
public string Controller ;
18
19
public string Action ;
19
20
public string View ;
21
+ public string RedirectUrl ;
20
22
public bool Sent ;
21
23
22
24
public Dictionary < string , string > Post ;
@@ -37,13 +39,14 @@ public WebContext(HttpListenerContext context, string sessionSavePath)
37
39
this . context = context ;
38
40
this . sessionSavePath = sessionSavePath ;
39
41
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 ( ) ;
44
46
45
- Data = new System . Dynamic . ExpandoObject ( ) ;
46
- Sent = false ;
47
+ this . Data = new ViewData ( ) ;
48
+ this . RedirectUrl = null ;
49
+ this . Sent = false ;
47
50
}
48
51
49
52
private String CreateSessionIdentifier ( )
@@ -157,6 +160,11 @@ FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileSh
157
160
return null ;
158
161
}
159
162
163
+ public void Redirect ( string url )
164
+ {
165
+ this . RedirectUrl = url ;
166
+ }
167
+
160
168
private void ReadPostData ( )
161
169
{
162
170
HttpListenerRequest request = this . context . Request ;
@@ -174,9 +182,19 @@ private void ReadPostData()
174
182
175
183
}
176
184
177
- internal void SendString ( string output , int StatusCode = 200 )
185
+ internal void SendString ( string output , string mimeType = "text/html" , int StatusCode = 200 )
178
186
{
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 ;
180
198
byte [ ] buf = System . Text . Encoding . UTF8 . GetBytes ( output ) ;
181
199
this . context . Response . ContentLength64 = buf . Length ;
182
200
this . context . Response . OutputStream . Write ( buf , 0 , buf . Length ) ;
0 commit comments