I want local compiler errors to apply to methods that need to be changed after upgrading to the .NET Framework 4.0. See below:
Oops, although this is just an example, this code does not fully work, which I just realized. (A patched version is available at the end if you ever need this functionality)
// ASP.NET 3.5 does not contain a global accessor for routing data // this workaround will be used until we transition to .NET 4.0 // this field still exists in .NET 4.0, however, it is never used // GetRouteData will always return null after the transition to .NET 4.0 static object _requestDataKey = typeof(UrlRoutingModule) .GetField("_requestDataKey", BindingFlags.NonPublic | BindingFlags.Instance) .GetValue(null) ; public static RouteData GetRouteData(this HttpContext context) { if (context != null) { return context.Items[_requestDataKey] as RouteData; } // .NET 4.0 equivalent //if ((context != null) && (context.Request != null)) //{ // return context.Request.RequestContext.RouteData; //} return null; }
Does anyone know of a trick that leads to a compiler error after the transition?
This code actually does what the original version intended. This code also does not apply to version 3.5 at run time, however there are even simpler ways to get route data in 4.0.
if (context != null) { var routeData = context.Items["RouteData"] as RouteData; if (routeData == null && !context.Items.Contains("RouteData")) { context.Items["RouteData"] = routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(context)); } return routeData; }
source share