//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); }
/// <summary> /// Adds nested route with custom action name. Eg: to add parentCategory/{parentId}/actionName/{childCategoryId} /// eg: Fund/{fundId}/investorcommitments/{investorId} /// </summary> /// <param name="routes"></param> /// <param name="actionName"></param> /// <param name="parentCategory"></param> /// <param name="childCategory"></param> /// <param name="act"></param> public static void AddNestedActionRoute(RouteCollection routes, string actionName, string parentCategory, string childCategory, RouteAction act) { // Fund/fundId/Action/childCategoryId string parentCategoryIdName = "id"; string route = string.Empty; route = parentCategory + "/{" + parentCategoryIdName + "}/" + actionName + "/{" + childCategory + "Id}"; string route_name = RouteNames.GetActionRouteName(actionName, parentCategory, childCategory, act); _routeNameToRoute.Add(route_name, routes.MapRouteWithName( route_name, // Route name route, // URL with parameters new { controller = parentCategory, action = actionName }, // Parameter defaults new { httpMethod = new HttpMethodConstraint(GetHttpVerb(act)) } )); }
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)) } )); }
public static void ConfigureWebApiRoutes(RouteCollection routes) { string defaultResourceName = RouteNames.DefaultResource; // Search string route_name = RouteNames.GetRouteName(defaultResourceName, RouteAction.SEARCH); _routeNameToRoute.Add(route_name, routes.MapRouteWithName( string.Format(route_name), // Route name "{controller}/search", // URL with parameters new { action = "Search" }, // Parameter defaults new { httpMethod = new HttpMethodConstraint("GET") } )); // New route // GET /Photo => Get() //route_name = RouteNames.GetRouteName(defaultResourceName, RouteAction.LIST); //_routeNameToRoute.Add(route_name, routes.MapRouteWithName( // string.Format(route_name), // Route name // "{controller}", // URL with parameters // new { action = "Get" }, // Parameter defaults // new { httpMethod = new HttpMethodConstraint("GET") } // )); // GET /Photo/1 => Get(1) //string route_name = "get_default"; //route_name = RouteNames.GetRouteName(defaultResourceName, RouteAction.SHOW); //_routeNameToRoute.Add(route_name, routes.MapRouteWithName( // string.Format(route_name), // Route name // "{controller}/{id}", // URL with parameters // new {action = "Get", id = UrlParameter.Optional}, // Parameter defaults // new {httpMethod = new HttpMethodConstraint("GET")} // )); route_name = RouteNames.GetRouteName(defaultResourceName, RouteAction.SHOW); _routeNameToRoute.Add(route_name, routes.MapRouteWithName( string.Format(route_name), // Route name "{controller}/{id}", // URL with parameters new { action = "Get" }, // Parameter defaults new { httpMethod = new HttpMethodConstraint("GET") } )); // Create // POST /Photo => POST() or POST(Photo photo) route_name = "create_default"; route_name = RouteNames.GetRouteName(defaultResourceName, RouteAction.CREATE); _routeNameToRoute.Add(route_name, routes.MapRouteWithName( string.Format(route_name), // Route name "{controller}", // URL with parameters new { action = "Post" }, // Parameter defaults new { httpMethod = new HttpMethodConstraint("POST") } )); // UPDATE // POST or PUT /Photo/1 => Put(int id, Photo photo) route_name = "update_default"; route_name = RouteNames.GetRouteName(defaultResourceName, RouteAction.UPDATE, false); _routeNameToRoute.Add(route_name, routes.MapRouteWithName( string.Format(route_name), // Route name "{controller}/{id}", // URL with parameters new { action = "Put" }, // Parameter defaults new { httpMethod = new HttpMethodConstraint("PUT") })); route_name = "update_default_low_rest"; route_name = RouteNames.GetRouteName(defaultResourceName, RouteAction.UPDATE); _routeNameToRoute.Add(route_name, routes.MapRouteWithName( string.Format(route_name), // Route name "{controller}/{id}", // URL with parameters new { action = "Put" }, // Parameter defaults new { httpMethod = new HttpMethodConstraint("POST") } )); // DELETE // GET /Photo/1/delete => delete(int id) route_name = "delete_default_lowrest"; route_name = RouteNames.GetRouteName(defaultResourceName, RouteAction.DELETE, true); _routeNameToRoute.Add(route_name, routes.MapRouteWithName( string.Format(route_name), // Route name "{controller}/{id}/delete", // URL with parameters new { action = "Delete" }, // Parameter defaults new { httpMethod = new HttpMethodConstraint("GET") } )); // DELETE /Photo/id => delete(int id) route_name = "delete_default"; route_name = RouteNames.GetRouteName(defaultResourceName, RouteAction.DELETE, false); _routeNameToRoute.Add(route_name, routes.MapRouteWithName( string.Format(route_name), // Route name "{controller}/{id}", // URL with parameters new { action = "Delete" }, // Parameter defaults new { httpMethod = new HttpMethodConstraint("DELETE") } )); routes.MapRouteWithName( "Default", "{controller}/{action}", new { controller = "Home", action = "Index" }, null ); isInitialized = true; }