コード例 #1
0
        //private static void AddLog(string log) {
        //    try {
        //        string msg = string.Empty;
        //        string fileName = "";
        //        if(System.IO.File.Exists(fileName)){
        //            msg =  System.IO.File.ReadAllText(fileName);
        //        }
        //        msg = msg + log + Environment.NewLine + Environment.NewLine + Environment.NewLine;
        //        System.IO.File.WriteAllText(fileName, msg);
        //    } catch { }
        //}

        public static string GetNestedRouteUrl(string parentCategory, string childCategory, RouteAction act, out string action, string pluralChildCategory = "", bool isLowRest = true)
        {
            string childCategoryName = RouteNames.GetChildResourceName(childCategory, act);

            action = string.Empty;
            string route = string.Empty;
            //string parentCategoryIdName = parentCategory + "Id";
            string parentCategoryIdName = "id";

            // LIST
            //  GET     Album/10/photos Get all the photos in the album
            //  get_photos_in_album
            if (act == RouteAction.LIST)
            {
                route  = parentCategory + "/{" + parentCategoryIdName + "}/" + childCategoryName;
                action = childCategoryName;
            }


            //  Show
            //  GET     Album/10/photo/1	display a specific photo
            //  get_photo_in_album
            if (act == RouteAction.SHOW)
            {
                route = parentCategory + "/{" + parentCategoryIdName + "}/" + childCategoryName + "/{" +
                        childCategoryName +
                        "Id}";
                action = childCategoryName;
            }



            //POST  Album/10/photo/1    Photos  update  update a specific photo
            if (act == RouteAction.UPDATE)
            {
                route = parentCategory + "/{" + parentCategoryIdName + "}/" + childCategoryName + "/{" + childCategory +
                        "Id}";
                action = "Update" + childCategoryName;
            }

            // POST     Album/10/photo  create  create a new photo
            if (act == RouteAction.CREATE)
            {
                route  = parentCategory + "/{" + parentCategoryIdName + "}/" + childCategoryName;
                action = childCategoryName;
            }


            //GET   Album/10/photo/1/delete  delete a photo
            if (act == RouteAction.DELETE)
            {
                route = parentCategory + "/{" + parentCategoryIdName + "}/" + childCategory + "/{" + childCategory +
                        "Id}";
                action = "Delete" + childCategoryName;
            }
            return(route);
        }
コード例 #2
0
        private static void AddRoute(RouteCollection routes, string parentCategory, string childCategory, RouteAction act, string pluralChildCategory = "", bool isLowRest = true)
        {
            if (!string.IsNullOrEmpty(pluralChildCategory))
            {
                RouteNames.pluralResourceNames.Add(parentCategory, pluralChildCategory);
            }

            string childCategoryName = RouteNames.GetChildResourceName(childCategory, act);
            string action            = string.Empty;
            string route             = GetNestedRouteUrl(parentCategory, childCategory, act, out action, pluralChildCategory,
                                                         isLowRest);



            string route_name = RouteNames.GetNestedRouteName(parentCategory, childCategoryName, act, isLowRest);

            //AddLog(parentCategory + "/" + action + "->" + route + "/" + route_name);
            _routeNameToRoute.Add(route_name, routes.MapRouteWithName(
                                      route_name,                                           // Route name
                                      route,                                                // URL with parameters
                                      new { controller = parentCategory, action = action }, // Parameter defaults
                                      new { httpMethod = new HttpMethodConstraint(GetHttpVerb(act)) }
                                      ));
        }