0

I'm trying to programmatically create a route in ASP.NET Web Api that s

config.Routes.MapHttpRoute(
   name: "test",
   routeTemplate: "api/foo",
   defaults: new
   {
       controller = "Foo",
       action = "Test"
   },
   constraints: new
   {
         httpMethod = new HttpMethodConstraint(HttpMethod.Get)
   });

However, if I try to invoke the method with a GET I get the following response.

{
Message: "The requested resource does not support http method 'GET'."
}

If I switch the constraint to POST and invoke with a POST, all works well (and the action shows on the help page).

Any ideas what I'm doing wrong?

ConfusedNoob
  • 9,826
  • 14
  • 64
  • 85

1 Answers1

2

Why not just use conventions and http verbs.

Ex: Define actions called Get, Post, Put etc and just hit them with the root part of the url and use http verbs to tell WEB Api which action to respond with.

TGH
  • 38,769
  • 12
  • 102
  • 135
  • You mean have one method for each possible HttpMethod and vary the action based on the metadata? – ConfusedNoob Jul 03 '14 at 03:21
  • Yes, just let web api handle the routing to the correct method based on the correct http verb in the request. – TGH Jul 03 '14 at 03:22
  • Since the code in each method would be identical - I wonder if a better way would be to have on method and attribute it with all.. [HttpGet], [HttpPost] etc. Then it can handle any and I can enforce constraints on routes as appropriate – ConfusedNoob Jul 03 '14 at 03:28
  • Ok, however, I believe no attribute will mean the same as listing them all out. – TGH Jul 03 '14 at 03:30