Route类提供了五个属性,并且包装了一个ParsedRoute对象(即已解析过的Route规则),通过察看Route类的构造函数和Url属性的Set方法,我们可以知道的是:Route规则是在添加的时候就解析的,即,在Route类事例化的时候设置Url属性的时候就生成了ParsedRoute对象。
其他的四个属性分别是 :默认值,约束规则,自带数据,处理Handler.
可以把Router类对比和RouterData类作对比。。。。
Route类的四个方法我将会在 Routing类(下)中介绍。
using System; using System.Collections.Generic; using System.Globalization; using System.Security.Permissions; using System.Text.RegularExpressions; using System.Web; namespace System.Web.Routing { public enum RouteDirection { IncomingRequest, UrlGeneration } [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class Route : RouteBase { // Fields private ParsedRoute _parsedRoute; private string _url; private const string HttpMethodParameterName = "httpMethod"; Constructions#region Constructions public Route(string url, IRouteHandler routeHandler) { this.Url = url; this.RouteHandler = routeHandler; } public Route(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) { this.Url = url; this.Defaults = defaults; this.RouteHandler = routeHandler; } public Route(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler) { this.Url = url; this.Defaults = defaults; this.Constraints = constraints; this.RouteHandler = routeHandler; } public Route(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) { this.Url = url; this.Defaults = defaults; this.Constraints = constraints; this.DataTokens = dataTokens; this.RouteHandler = routeHandler; } #endregion public override RouteData GetRouteData(HttpContextBase httpContext) { string virtualPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo; RouteValueDictionary values = this._parsedRoute.Match(virtualPath, this.Defaults); if (values == null) return null; RouteData data = new RouteData(this, this.RouteHandler); if (!this.ProcessConstraints(httpContext, values, RouteDirection.IncomingRequest)) return null; foreach (KeyValuePair<string, object> pair in values) data.Values.Add(pair.Key, pair.Value); if (this.DataTokens != null) foreach (KeyValuePair<string, object> pair2 in this.DataTokens) data.DataTokens[pair2.Key] = pair2.Value; return data; } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { BoundUrl url = this._parsedRoute.Bind(requestContext.RouteData.Values, values, this.Defaults, this.Constraints); if (url == null) return null; if (!this.ProcessConstraints(requestContext.HttpContext, url.Values, RouteDirection.UrlGeneration)) return null; VirtualPathData data = new VirtualPathData(this, url.Url); if (this.DataTokens != null) foreach (KeyValuePair<string, object> pair in this.DataTokens) data.DataTokens[pair.Key] = pair.Value; return data; } protected virtual bool ProcessConstraint(HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { object obj2; IRouteConstraint constraint2 = constraint as IRouteConstraint; if (constraint2 != null) return constraint2.Match(httpContext, this, parameterName, values, routeDirection); string str = constraint as string; if (str == null) throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources. Route_ValidationMustBeStringOrCustomConstraint, new object[] {parameterName, this.Url})); values.TryGetValue(parameterName, out obj2); string input = Convert.ToString(obj2, CultureInfo.InvariantCulture); string pattern = "^(" + str + ")$"; return Regex.IsMatch(input, pattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); } private bool ProcessConstraints(HttpContextBase httpContext, RouteValueDictionary values, RouteDirection routeDirection) { if (this.Constraints != null) foreach (KeyValuePair<string, object> pair in this.Constraints) if (!this.ProcessConstraint(httpContext, pair.Value, pair.Key, values, routeDirection)) return false; return true; } // Properties public RouteValueDictionary Constraints { get; set; } public RouteValueDictionary DataTokens { get; set; } public RouteValueDictionary Defaults { get; set; } public IRouteHandler RouteHandler { get; set; } public string Url { get { return (this._url ?? string.Empty); } set { this._parsedRoute = RouteParser.Parse(value); this._url = value; } } } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, return
, this
, public
, values
, httpcontext
Constraints
system.web.routing、system命名空间、解析带命名空间的xml、xml 解析去掉命名空间、数字电影命名解析,以便于您获取更多的相关知识。
时间: 2024-11-03 14:00:56