-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRouteResolver.cs
More file actions
186 lines (149 loc) · 5.46 KB
/
Copy pathRouteResolver.cs
File metadata and controls
186 lines (149 loc) · 5.46 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#if NETFULL
using System;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;
namespace StackifyLib.Web
{
/// <summary>
/// Can be used to figure out the MVC route for a web request
/// </summary>
public class RouteResolver
{
public class Route
{
public string Area { get; set; }
public string Action { get; set; }
public string Controller { get; set; }
public string Pattern { get; set; }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (!string.IsNullOrEmpty(Area))
{
sb.Append(Area + ".");
}
if (!string.IsNullOrEmpty(Controller))
{
sb.Append(Controller + ".");
}
if (!string.IsNullOrEmpty(Action))
{
sb.Append(Action);
}
return sb.ToString().Trim('.');
}
}
private HttpContextBase _Context = null;
Route _Route = new Route();
public RouteResolver()
: this(new HttpContextWrapper(System.Web.HttpContext.Current))
{
}
public RouteResolver(HttpContextBase context)
{
_Context = context;
_Route = EvalRoute();
}
private bool _IISAppended = false;
public Route GetRoute()
{
return _Route;
}
/// <summary>
/// Easy to way to log the MVC route to your IIS log
/// </summary>
public void AppendToIISLog()
{
try
{
if (_IISAppended) return; //don't do this twice
if (_Context == null) return;
if (_Context.Items != null && _Context.Items["StackifyAppendToLogSet"] != null) return;
string routeKey = _Route.ToString();
if (!string.IsNullOrEmpty(routeKey))
{
_Context.Response.AppendToLog("&ResolvedRoute={" + routeKey.Trim('.').Replace(" ", "-") + "}");
_Context.Items["StackifyAppendToLogSet"] = true;
_IISAppended = true;
}
}
catch (Exception e)
{
StackifyLib.Utils.StackifyAPILogger.Log("Error Appending route to IIS log " + e.ToString());
}
}
public static void AppendToIISLog(HttpContext context, string route)
{
if (context == null) return;
if (context.Items != null && context.Items["StackifyAppendToLogSet"] != null) return;
if (!string.IsNullOrEmpty(route))
{
context.Response.AppendToLog("&ResolvedRoute={" + route.Trim('.').Replace(" ", "-") + "}");
context.Items["StackifyAppendToLogSet"] = true;
}
}
public void CreateServerVariables()
{
if (!string.IsNullOrEmpty(_Route.Pattern))
{
_Context.Request.ServerVariables["ROUTE_PATTERN"] = _Route.Pattern;
}
if (!string.IsNullOrEmpty(_Route.Area))
{
_Context.Request.ServerVariables["ROUTE_AREA"] = _Route.Area;
}
if (!string.IsNullOrEmpty(_Route.Controller))
{
_Context.Request.ServerVariables["ROUTE_CONTROLLER"] = _Route.Controller;
}
if (!string.IsNullOrEmpty(_Route.Action))
{
_Context.Request.ServerVariables["ROUTE_ACTION"] = _Route.Action;
}
}
private Route EvalRoute()
{
Route route = new Route();
try
{
if (_Context == null) return route;
if (_Context == null || RouteTable.Routes == null) return route;
var routeData = RouteTable.Routes.GetRouteData(_Context);
if (routeData != null && routeData.Values != null && routeData.Values.Any())
{
if (routeData.Route is System.Web.Routing.Route)
{
route.Pattern =((System.Web.Routing.Route)(routeData.Route)).Url;
}
if (routeData.DataTokens != null && routeData.DataTokens.ContainsKey("area"))
{
route.Area = (string)routeData.DataTokens["area"];
}
if (routeData.Values != null)
{
if (string.IsNullOrEmpty(route.Area) && routeData.Values.ContainsKey("area"))
{
route.Area = (string) routeData.Values["area"];
}
if (routeData.Values.ContainsKey("controller"))
{
route.Controller = (string) routeData.Values["controller"];
}
if (routeData.Values.ContainsKey("action"))
{
route.Action = (string) routeData.Values["action"];
}
}
}
}
catch (Exception e)
{
StackifyLib.Utils.StackifyAPILogger.Log("Error resolving route " + e.ToString());
}
return route;
}
}
}
#endif