I have two action methods in my Products controller. This is my RouteConfig.
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
These are the two actions and their working urls.
[HttpGet]
//uri:http://localhost:49964/api/products/product?strKey=1
public IHttpActionResult Product(string strKey)
[HttpPost]
//uri:http://localhost:49964/api/products/product
public IHttpActionResult Product([FromBody] Product product)
But I also want to use the below url for GET.
http://localhost:49964/api/products/product/1
But web api responds with,
The requested resource does not support http method 'GET'.
Change strKey
to id
or do the reverse if you want to keep strKey
.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{strKey}",
defaults: new { strKey = RouteParameter.Optional }
);
The route template needs to match up to the action for mapping to work as intended.
//GET api/products/product/1
//GET api/products/product?strKey=1
[HttpGet]
public IHttpActionResult Product(string strKey)
this would however mean that all actions in this route would optionally use strKey
as a placeholder