/// <summary> /// 判断是否符合规则。 /// </summary> /// <param name="context">HttpContext 对象,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session 和 Server)的引用。</param> /// <param name="config">符合的配置项。</param> /// <param name="simplePath">相对于应用程序根目录的路径。</param> /// <param name="realPath">相对于应用程序根目录的实际需调用文件路径。</param> /// <param name="filePath">相对于应用程序根目录的静态文件路径。</param> /// <param name="pathInfo">源的附加路径信息。 </param> /// <param name="queryString">请求查询字符串。</param> /// <param name="url">重定向URL。</param> /// <returns></returns> public static bool IsMatch(HttpContext context, ref UrlManagerConfig config, ref string simplePath, ref string realPath, ref string filePath, ref string pathInfo, ref string queryString, ref string url) { string simpleUrl = context.Request.RawUrl.Substring((context.Request.ApplicationPath == "/") ? (context.Request.ApplicationPath.Length - 1) : context.Request.ApplicationPath.Length); simplePath = context.Request.Path.Substring((context.Request.ApplicationPath == "/") ? (context.Request.ApplicationPath.Length - 1) : context.Request.ApplicationPath.Length); List <UrlManagerConfig> defaultList = GetConfigs("none"); if (defaultList != null) { foreach (UrlManagerConfig item in defaultList) { Regex reg = new Regex(item.Pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); Match m = reg.Match(simpleUrl); if (m.Success) { config = item; string[] strs = new string[m.Groups.Count]; for (int i = 0; i < m.Groups.Count; i++) { strs[i] = m.Groups[i].Value; } realPath = string.Format(item.RealPath, strs); filePath = FormatFilePath(context, string.Format(item.FilePath, strs)); pathInfo = string.Format(item.PathInfo, strs); queryString = string.Format(item.QueryString, strs); url = string.Format(item.Url, strs); // realPath = context.Request.Path; // filePath = context.Request.Path; // pathInfo = context.Request.PathInfo; // queryString = context.Request.QueryString.ToString(); return(true); } } } List <UrlManagerConfig> list = GetConfigs(context.Request.Url.Host); if (list != null) { foreach (UrlManagerConfig item in list) { Regex reg = new Regex(item.Pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); Match m = reg.Match(simpleUrl); if (m.Success) { config = item; string[] strs = new string[m.Groups.Count]; for (int i = 0; i < m.Groups.Count; i++) { strs[i] = m.Groups[i].Value; } realPath = string.Format(item.RealPath, strs); filePath = FormatFilePath(context, string.Format(item.FilePath, strs)); pathInfo = string.Format(item.PathInfo, strs); queryString = string.Format(item.QueryString, strs); url = string.Format(item.Url, strs); return(true); } } } return(false); }
/// <summary> /// 从XML定义节点返回定义对象。 /// </summary> /// <param name="node">XML节点。</param> /// <param name="host">主机名。</param> /// <returns>对象。</returns> public static UrlManagerConfig GetFromXmlNode(XmlNode node, string host) { UrlManagerConfig item = new UrlManagerConfig(); item.Host = host; if (node.Attributes["action"] != null && node.Attributes["action"].Value != null && node.Attributes["action"].Value.Length > 0) { item.Action = node.Attributes["action"].Value; } if (node.Attributes["pattern"] != null && node.Attributes["pattern"].Value != null && node.Attributes["pattern"].Value.Length > 0) { item.Pattern = node.Attributes["pattern"].Value; } if (node.Attributes["url"] != null && node.Attributes["url"].Value != null && node.Attributes["url"].Value.Length > 0) { item.Url = node.Attributes["url"].Value; } if (node.Attributes["realpath"] != null && node.Attributes["realpath"].Value != null && node.Attributes["realpath"].Value.Length > 0) { item.RealPath = node.Attributes["realpath"].Value; } if (node.Attributes["filepath"] != null && node.Attributes["filepath"].Value != null && node.Attributes["filepath"].Value.Length > 0) { item.FilePath = node.Attributes["filepath"].Value; } if (node.Attributes["pathinfo"] != null && node.Attributes["pathinfo"].Value != null && node.Attributes["pathinfo"].Value.Length > 0) { item.PathInfo = node.Attributes["pathinfo"].Value; } if (node.Attributes["querystring"] != null && node.Attributes["querystring"].Value != null && node.Attributes["querystring"].Value.Length > 0) { item.QueryString = node.Attributes["querystring"].Value; } if (node.Attributes["timespan"] != null && node.Attributes["timespan"].Value != null && node.Attributes["timespan"].Value.Length > 0) { item.TimeSpan = TimeSpan.Parse(node.Attributes["timespan"].Value); } return(item); }