-
Notifications
You must be signed in to change notification settings - Fork 0
/
Global.asax.cs
170 lines (139 loc) · 5.26 KB
/
Global.asax.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Desharp;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace WebApp {
// https://docs.microsoft.com/cs-cz/dotnet/api/system.web.httpapplication?view=netframework-4.8
public partial class Global : System.Web.HttpApplication, IHttpHandler, IRequiresSessionState {
/**
* Global.aspx HttpApplication handlers:
*/
// Called only once per session:
protected void Session_Start(object o, EventArgs e) {
HttpContext.Current.Session.Add("RequestCounter", 0);
}
// Change rewritten url back to raw url at the request begin:
protected void Application_BeginRequest (object o, EventArgs e) {
this.Request
.GetType()
.GetField("_url", BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(this.Request, new System.Uri(
this.Request.Url.Scheme + System.Uri.SchemeDelimiter +
this.Request.Url.Authority + this.Request.RawUrl
));
}
// Read session data:
protected void Application_PostAuthorizeRequest (object o, EventArgs e) {
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
// Handle all requests execution:
protected void Application_PreRequestHandlerExecute (object o, EventArgs e) {
if (this.Request.Url.LocalPath.IndexOf("/test-redirect") == 0) {
this._demoHandleTestRecorect();
} else {
this._demoHandleAllRequests();
}
}
// Add execution time header after everything is done:
protected void Application_PostRequestHandlerExecute (object o, EventArgs e) {
TimeSpan reqTimeSpan = DateTime.Now - HttpContext.Current.Timestamp;
string requestTime = reqTimeSpan.Milliseconds.ToString("0.###", new CultureInfo("en-US")) + " ms ("
+ reqTimeSpan.Ticks.ToString() + " ticks)";
this.Response.AddHeader("X-Exec-Time", requestTime);
}
/**
* Demo handler methods:
*/
// Handle demo redirect request:
private void _demoHandleTestRecorect () {
Debug.Dump("Redirected from: " + this.Request.Url.AbsoluteUri);
this.Response.Headers.Add("Location", "/");
this.Response.StatusCode = 302;
}
// Handle all demo requests:
private void _demoHandleAllRequests () {
// Write some standard output:
this.Response.Write("Hallo world!<br /><br />");
// Try to dump something in debug mode:
try {
this._demoDumpAndLog();
this._demoSession();
//this._demoCatchedException();
//this._demoUncatchedException();
} catch (Exception ex) {
// Last exception is:
// - always displayed over screen in debug mode:
// - always logged automatically in non-debug mode:
throw new Exception("Global request error.", ex);
}
// Run some test dumps:
//this._runDumpingTests();
//this._runExceptionsTests();
// Write some standard output:
this.Response.Write(@"<br /><br />Click for <a href=""/test-redirect"">demo redirection</a>.");
}
/**
* Demo dump methods:
*/
// Dump some structured example data:
private void _demoDumpAndLog() {
if (!Debug.Enabled()) return;
var demoObject = new Dictionary<string, object>() {
{ "clark", new {
name = "Clark",
surname = "Kent",
tshirtIdol = "chuck"
} },
{ "chuck", new {
name = "Chuck",
surname = "Noris",
tshirtIdol = "bud"
} },
{ "bud", new {
name = "Bud",
surname = "Spencer",
tshirtIdol = ""
} }
};
string dumpedObject = Debug.Dump(demoObject, new DumpOptions {
Return = true,
SourceLocation = true
});
this.Response.Write(dumpedObject);
Debug.Dump(demoObject);
Debug.Log(demoObject);
}
// Count something in session:
private void _demoSession() {
int requestsCount = (int)this.Session["RequestCounter"];
this.Session["RequestCounter"] = requestsCount + 1;
}
// Render some catched exception:
private void _demoCatchedException () {
try {
throw new Exception("Demo catched exception text.");
} catch (Exception ex) {
Debug.Dump(ex);
Debug.Log(ex);
}
}
// Render some uncatched exception:
private void _demoUncatchedException () {
throw new Exception("Demo uncatched exception text.");
}
// Run test dumps:
private void _runDumpingTests () {
var dlTest = new Desharp.Tests.DumpingAndLoging();
dlTest.TestAll();
}
// Run test exceptions rendering:
private void _runExceptionsTests () {
var eTest = new Desharp.Tests.ExceptionsRendering();
eTest.TestAll();
}
}
}