Skip to content

Instantly share code, notes, and snippets.

@carolynvs
Last active June 12, 2020 18:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carolynvs/4444243 to your computer and use it in GitHub Desktop.
Save carolynvs/4444243 to your computer and use it in GitHub Desktop.
ASP.NET Route configuration demonstrating how to mix REST and RPC style actions on the same controller.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// GET /api/{resource}/{action}
routes.MapHttpRoute(
name: "Web API RPC",
routeTemplate: "api/{controller}/{action}",
defaults: new { },
constraints: new { action = @"[A-Za-z]+", httpMethod = new HttpMethodConstraint("GET") }
);
// GET|PUT|DELETE /api/{resource}/id
routes.MapHttpRoute(
name: "Web API Resource",
routeTemplate: "api/{controller}/{id}",
defaults: new { },
constraints: new { id = @"\d+" }
);
// GET /api/{resource}
routes.MapHttpRoute(
name: "Web API Get All",
routeTemplate: "api/{controller}",
defaults: new { action = "Get" },
constraints: new { httpMethod = new HttpMethodConstraint("GET") }
);
// POST /api/{resource}
routes.MapHttpRoute(
name: "Web API Post",
routeTemplate: "api/{controller}",
defaults: new { action = "Post" },
constraints: new { httpMethod = new HttpMethodConstraint("POST") }
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment