Browse files

Changed the route debugger to look for a hidden __DebugRoute property…

… on routes that are of a type other than Route and use that for debug information.
  • Loading branch information...
1 parent 716af49 commit fc67b0e07041816f7f4e9b3116b8656df6be6ed5 Haacked committed Jul 16, 2011
View
18 src/RouteDebug/DebugHttpHandler.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Reflection;
using System.Web;
using System.Web.Routing;
@@ -100,7 +101,8 @@ public class DebugHttpHandler : IHttpHandler {
string constraints = "n/a";
string dataTokens = "n/a";
- Route route = routeBase as Route;
+ Route route = CastRoute(routeBase);
+
if (route != null) {
url = route.Url;
defaults = FormatDictionary(route.Defaults);
@@ -148,6 +150,20 @@ public class DebugHttpHandler : IHttpHandler {
, generatedUrlInfo));
}
+ private Route CastRoute(RouteBase routeBase) {
+ var route = routeBase as Route;
+ if (route == null) {
+ // cheat!
+ // TODO: Create an interface for self reporting routes.
+ var type = routeBase.GetType();
+ var property = type.GetProperty("__DebugRoute", BindingFlags.NonPublic | BindingFlags.Instance);
+ if (property != null) {
+ route = property.GetValue(routeBase, null) as Route;
+ }
+ }
+ return route;
+ }
+
private static string FormatDictionary(IDictionary<string, object> values) {
if (values == null)
return "(null)";
View
5 src/RouteDebuggerDemoWeb/Global.asax.cs
@@ -1,14 +1,15 @@
using System;
using System.Web.Mvc;
using System.Web.Routing;
+using RouteMagic;
namespace RouteTesterDemoWeb {
public class GlobalApplication : System.Web.HttpApplication {
public static void RegisterRoutes(RouteCollection routes) {
var route = routes.MapRoute("foo-route", "foo/{id}", new { controller = "Test", action = "Index", id = UrlParameter.Optional });
route.DataTokens = new RouteValueDictionary(new { routeName = "foo-route" });
- routes.MapRoute("bar-route", "bar/{id}", new { controller = "Test", action = "Index", id = (string)null });
- routes.MapRoute("token-route", "tokens/{id}", new { dataToken = "BlahBlahBlah" });
+ routes.Map("bar-route", "bar/{id}", new { controller = "Test", action = "Index", id = (string)null });
+ routes.Map("token-route", "tokens/{id}", new { dataToken = "BlahBlahBlah" });
routes.MapRoute("extension", "ext/{id}.mvc", new { controller = "Home", action = "Index", id = (string)null });
routes.MapRoute("mvc-default", "{controller}/{action}/{id}"
, new { controller = "Test", action = "Index", id = (string)null });
View
4 src/RouteDebuggerDemoWeb/RouteDebuggerDemoWeb.csproj
@@ -90,6 +90,10 @@
<Project>{09548C4E-F0C1-428A-BC3A-BFBB9328D7C7}</Project>
<Name>RouteDebugger</Name>
</ProjectReference>
+ <ProjectReference Include="..\RouteMagic.Mvc\RouteMagic.Mvc.csproj">
+ <Project>{D9321DA3-F3AC-4567-8452-B1B5D5633045}</Project>
+ <Name>RouteMagic.Mvc</Name>
+ </ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
View
9 src/RouteMagic/Internals/NormalizeRoute.cs
@@ -12,12 +12,13 @@ public NormalizeRoute(RouteBase route)
if (route == null) {
throw new ArgumentNullException("route");
}
- OriginalRoute = route;
+ __DebugRoute = route;
AppendTrailingSlash = appendTrailingSlash;
RequireLowerCase = requireLowerCase;
}
- public RouteBase OriginalRoute {
+ // This is so the RouteDebugger reports on this correctly.
+ internal RouteBase __DebugRoute {
get;
private set;
}
@@ -33,7 +34,7 @@ public NormalizeRoute(RouteBase route)
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
- var vpd = OriginalRoute.GetVirtualPath(requestContext, values);
+ var vpd = __DebugRoute.GetVirtualPath(requestContext, values);
if (vpd != null) {
var virtualPath = vpd.VirtualPath;
if (RequireLowerCase) {
@@ -57,7 +58,7 @@ public NormalizeRoute(RouteBase route)
}
public override RouteData GetRouteData(HttpContextBase httpContext) {
- return OriginalRoute.GetRouteData(httpContext);
+ return __DebugRoute.GetRouteData(httpContext);
}
}
}

0 comments on commit fc67b0e

Please sign in to comment.