-
-
Save iggym/6923596 to your computer and use it in GitHub Desktop.
Web API Routing notes and example
This file contains hidden or 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
//Web API Routing notes and example | |
routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{action}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
//================ The Controller ========================================= | |
public class VTRoutingController : ApiController | |
{ | |
[ActionName("route")] | |
public MyResult PostRoute(MyRequestTemplate routingRequestTemplate) | |
{ | |
return null; | |
} | |
[ActionName("tspRoute")] | |
public MyResult PostTSPRoute(MyRequestTemplate routingRequestTemplate) | |
{ | |
return null; | |
} | |
} | |
//========= Route Configuration ================================ | |
// Controller Only | |
// To handle routes like `/api/VTRouting` | |
config.Routes.MapHttpRoute( | |
name: "ControllerOnly", | |
routeTemplate: "api/{controller}" | |
); | |
// Controller with ID | |
// To handle routes like `/api/VTRouting/1` | |
config.Routes.MapHttpRoute( | |
name: "ControllerAndId", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: null, | |
constraints: new { id = @"^\d+$" } // Only integers | |
); | |
// Controllers with Actions | |
// To handle routes like `/api/VTRouting/route` | |
config.Routes.MapHttpRoute( | |
name: "ControllerAndAction", | |
routeTemplate: "api/{controller}/{action}" | |
); | |
see http://stackoverflow.com/questions/11407267/multiple-httppost-method-in-mvc4-web-api-controller |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment