2

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'.
2
  • change strKey to id or do the reverse if you want to keep strKey. The route template needs to match up to the action for mapping to work as intended Commented Mar 4, 2018 at 18:16
  • but id is placeholder, right? like controller and action. Commented Mar 4, 2018 at 18:19

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thnx. I thought that id in route template is a placeholder.
it is a place holder in the route template. it is just not a reserved one like controller, action or area
I think the docs have never stressed on that 'reserved' part anywhere. I used to believe it to be a reserved placeholder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.