Skip to content

Instantly share code, notes, and snippets.

@mahizsas
Forked from carolynvs/RouteConfig.cs
Created January 28, 2013 11:30
Show Gist options
  • Save mahizsas/4654787 to your computer and use it in GitHub Desktop.
Save mahizsas/4654787 to your computer and use it in GitHub Desktop.
WebApi routing, that allow SOAP and REST style
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