Created
August 24, 2009 09:25
-
-
Save JeffreyZhao/173778 to your computer and use it in GitHub Desktop.
ParsedRoute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BoundUrl | |
{ | |
private static PropertyAccessor s_urlAccessor; | |
static BoundUrl() | |
{ | |
var type = typeof(Route).Assembly.GetType("System.Web.Routing.BoundUrl"); | |
var property = type.GetProperty("Url", BindingFlags.Instance | BindingFlags.Public); | |
s_urlAccessor = new PropertyAccessor(property); | |
} | |
private object m_instance; | |
public BoundUrl(object instance) | |
{ | |
this.m_instance = instance; | |
} | |
public string Url | |
{ | |
get | |
{ | |
return (string)s_urlAccessor.GetValue(this.m_instance); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class ParsedRoute | |
{ | |
private static MethodInvoker s_matchInvoker; | |
private static MethodInvoker s_bindInvoker; | |
static ParsedRoute() | |
{ | |
var routeType = typeof(Route).Assembly.GetType("System.Web.Routing.ParsedRoute"); | |
var matchMethod = routeType.GetMethod("Match", BindingFlags.Instance | BindingFlags.Public); | |
s_matchInvoker = new MethodInvoker(matchMethod); | |
var bindMethod = routeType.GetMethod("Bind", BindingFlags.Instance | BindingFlags.Public); | |
s_bindInvoker = new MethodInvoker(bindMethod); | |
} | |
private object m_instance; | |
public ParsedRoute(object instance) | |
{ | |
this.m_instance = instance; | |
} | |
public RouteValueDictionary Match( | |
string virtualPath, | |
RouteValueDictionary defaultValues) | |
{ | |
return (RouteValueDictionary)s_matchInvoker.Invoke(this.m_instance, virtualPath, defaultValues); | |
} | |
public BoundUrl Bind( | |
RouteValueDictionary currentValues, | |
RouteValueDictionary values, | |
RouteValueDictionary defaultValues, | |
RouteValueDictionary constraints) | |
{ | |
object boundUrl = s_bindInvoker.Invoke(this.m_instance, currentValues, values, defaultValues, constraints); | |
return boundUrl == null ? null : new BoundUrl(boundUrl); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static class RouteParser | |
{ | |
private static MethodInvoker s_parseInvoker; | |
static RouteParser() | |
{ | |
var parserType = typeof(Route).Assembly.GetType("System.Web.Routing.RouteParser"); | |
var parseMethod = parserType.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public); | |
s_parseInvoker = new MethodInvoker(parseMethod); | |
} | |
public static ParsedRoute Parse(string routeUrl) | |
{ | |
return new ParsedRoute(s_parseInvoker.Invoke(null, routeUrl)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
前辈您好,今天看了您之前发的【配合域名作URL Routing】的博文。想用这个来实现目前需用到的二级域名的功能。
奈何本人过于愚昧,对于底层知识也是知之甚少,反复看了几遍后,也只是把您这几个类还有FastReflectionLib给下载好确定没没啥错误提示了。
但是不知道在global中该怎么去写。比如我之前用的route是这样来写的:
routes.Add("cinfo" + 2, new Route("{MID}/{RootName}/{SecondName}/{id}.html", new RouteValueDictionary { { "MID", "MID" }, { "id", "id" }, { "test", "moban" } }, new UrlRouteHandler("/Template/0/cinfo.aspx")));
然后用您那个就不知道怎么去写了,望您能给指点一二,谢谢。