Last active
June 12, 2020 18:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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