Skip to content

Instantly share code, notes, and snippets.

@iggym
Forked from AlexZeitler/gist:1843616
Last active December 25, 2015 05:19
Show Gist options
  • Save iggym/6923596 to your computer and use it in GitHub Desktop.
Save iggym/6923596 to your computer and use it in GitHub Desktop.
Web API Routing notes and example
//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