3

I am trying to build a web API which would accept 2 parameters. However, upon calling the API it always hits the method without any parameters. I followed the instructions in here, and can't figure why it won't work.

The request I send using the 'PostMaster' Chrome extension : http://localhost:51403/api/test/title/bf

For the above request, I expect the first method to be hit, but the second method is being reached.

The method within the controller are :

// Get : api/test/type/slug
public void Get(string type,string slug){
//Doesn't reach here
}

// Get : api/test
public void Get() {
// Reaches here even when the api called is GET api/test/type/slug
}

The webApiConfig hasn't been changed much, except that it accepts 2 parameters :

public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id1}/{id2}",
            defaults: new { id1 = RouteParameter.Optional, id2 = RouteParameter.Optional }
        );
    }

My understanding from the documentation is that the webapiconfig doesn't have to be changed. Here's the error that I get

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:51403/api/test/title/bf'.",
"MessageDetail":"No action was found on the controller 'test' that matches the request."}
5
  • 2
    @kodi your routes don't match your request. If you rename the parameters to that method id1, and id2, what happens? Commented Jan 22, 2015 at 0:49
  • @GeorgeStocker I had no idea the argument name had to match . Thank you so much. That did it. Commented Jan 22, 2015 at 0:55
  • @GeorgeStocker : Is there a way to have the parameter names in webApiConfig to be more flexible, such that id1,id2 would automatically map to method parameters? Commented Jan 22, 2015 at 1:02
  • @GeorgeStocker Could you post this as an answer so I could accept it? Commented Jan 22, 2015 at 1:19
  • @kodi sure, I've also added a little more information. Commented Jan 22, 2015 at 2:37

2 Answers 2

3

In order for the routing engine to route the request to the right Action, it looks for the method whose parameters match the names in the route first.

In other words, this:

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id1}/{id2}",
            defaults: new { id1 = RouteParameter.Optional, id2 = RouteParameter.Optional }
);

matches:

public void Get(string id1, string id2) {}

but not:

public void Get(string type, string slug) {}

If you wanted to, this would have also worked:

http://localhost?type=weeeee&slug=herp-derp

which would have matched

public void Get(string type, string slug)
Sign up to request clarification or add additional context in comments.

Comments

0

You must use parameters name named id1 and id2 that in your route config. Like this:

// Get : api/test/type/slug
public void Get(string id1,string id2){
    //Doesn't reach here
}

4 Comments

Got it, thanks. Is there a way to configure webApiConfig such that I can have a generic path there, so I don't have to have id,id2 in the method?
the variable names are taken from the line the currently reads routeTemplate: "api/{controller}/{id1}/{id2}", change this to routeTemplate: "api/{controller}/{type}/{slug}"
Thanks @PaulCarroll : I was asking for a generic path because I would have an other method in a different controller which accepts 2 different parameter. Let's say : public void method2(string blah1,string blah2). I was wondering if there's a solution by which I could have something geenric in WebApiConfig, so I wouldn't have to keep changing it everytime I add a method.
You can use a complex object as the parameter,and set the route config. Like this: routeTemplate: "api/{controller}/{action}",

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.