Routing

public string GetRouteUrl(string routeName, string physicalFile, params object[] parames)
        {
            RouteBase routeBase = RouteTable.Routes[routeName];  // GetRouteData(new HttpContextWrapper(HttpContext.Current));
            if (routeBase != null)
            {
                Route route = (Route)routeBase;
                string url = route.Url;
                MatchCollection matchCollection = Regex.Matches(url, @"\w*\{name\d+\}\w*");
                string[] names = physicalFile.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < matchCollection.Count; i++)
                {
                    url = Regex.Replace(url, @"\w*\{name" + i + @"\d*\}\w*", names[i]);
                }
                url = Regex.Replace(url, @"\w*\{param\}\w*", parames[0].ToString());

                return "/"+url;
            }
            return null;
        }
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("Default", "", "~/Index.aspx");
            routes.MapPageRoute(
           "User",
           "{name0}/{name1}",
           "~/view/{name0}/{name1}.aspx"
           , false);

            routes.MapPageRoute(
           "User2",
           "{name0}/{name1}/{param}",
           "~/view/{name0}/{name1}.aspx"
           , false);
        }
posted @ 2011-03-21 17:04  凌鸢  阅读(280)  评论(0编辑  收藏  举报