public void GetRoutes(ICollection <RouteDescriptor> routes) { var displayPathsPerArea = _blueprint.HttpControllers.GroupBy( x => x.AreaName, x => x.Feature.Descriptor.Extension.Path); foreach (var item in displayPathsPerArea) { var areaName = item.Key; var displayPath = item.Distinct().Single(); var routeDescriptor = new HttpRouteDescriptor { Priority = -10, RouteTemplate = "api/" + displayPath + "/{controller}/{id}", Defaults = new { area = areaName, controller = "api", id = RouteParameter.Optional } }; routes.Add(routeDescriptor); } }
#pragma warning disable SA1204 // Static elements must appear before instance elements /// <summary> /// Creates a new <c>HttpRouteDescriptor</c>. Will parse the <paramref name="route"/> to add parameter-constrains. /// <example> /// "http://example.com/api/users/{id:int}" will create an integer contstraint for the id parameter. /// </example> /// </summary> public static HttpRouteDescriptor MakeRouteDescriptor(string area, string route, string controller, string action, int priority = 5, HttpMethod httpMethod = null) #pragma warning restore SA1204 // Static elements must appear before instance elements { var defaults = new ExpandoObject(); var underlyingObject = (IDictionary <string, object>)defaults; underlyingObject.Add("area", area); underlyingObject.Add("controller", controller); underlyingObject.Add("action", action); var constraints = ParseRouteAttributeConstraints(route, defaults, httpMethod); var routeDescriptor = new HttpRouteDescriptor { RouteTemplate = CleanupRoute(route), Defaults = defaults, Priority = priority, Constraints = constraints }; return(routeDescriptor); }
public IEnumerable <RouteDescriptor> GetRoutes() { var blogsRouteDescriptor = new HttpRouteDescriptor { Priority = -100, RouteTemplate = "api/blogs", Defaults = new { area = "RhymedCode.BlogsWebApi", controller = "Blogs", action = "Get" } }; var blogRouteDescriptor = new HttpRouteDescriptor { Priority = -100, RouteTemplate = "api/blogs/{blogId}", Defaults = new { area = "RhymedCode.BlogsWebApi", controller = "Blogs", action = "Get" } }; var blogPostsRouteDescriptor = new HttpRouteDescriptor { Priority = -100, RouteTemplate = "api/blogs/{blogId}/posts", Defaults = new { area = "RhymedCode.BlogsWebApi", controller = "Blogs", action = "Posts" } }; var blogPostRouteDescriptor = new HttpRouteDescriptor { Priority = -100, RouteTemplate = "api/posts/{id}", Defaults = new { area = "RhymedCode.BlogsWebApi", controller = "Posts", action = "GetPost" } }; var blogCommentsRouteDescriptor = new HttpRouteDescriptor { Priority = -100, RouteTemplate = "api/posts/{postId}/comments", Defaults = new { area = "RhymedCode.BlogsWebApi", controller = "Posts", action = "Comments" } }; var commentRouteDescriptor = new HttpRouteDescriptor { Priority = -100, RouteTemplate = "api/comments/{id}", Defaults = new { area = "RhymedCode.BlogsWebApi", controller = "Comments", action = "Get" } }; var latestCommentsRouteDescriptor = new HttpRouteDescriptor { Priority = -100, RouteTemplate = "api/comments/latest", Defaults = new { area = "RhymedCode.BlogsWebApi", controller = "Comments", action = "Latest" } }; return(new[] { blogsRouteDescriptor, blogRouteDescriptor, blogPostsRouteDescriptor, blogPostRouteDescriptor, blogCommentsRouteDescriptor, latestCommentsRouteDescriptor, commentRouteDescriptor, }); }