tom
tom

Reputation: 1822

Web api with two get actions

I have a Test controller with the following -

        public string Get(int id)
        {
            return "hi from test " + id;
        }

        [HttpGet]
        public string Search(string text)
        {
            return "you searched for  " + text;
        }

I can call

 - http://localhost:58635/api/Test/2
 - http://localhost:58635/api/Test?id=2
 - http://localhost:58635/api/Test/Search?text=textToSearcFor

but NOT http://localhost:58635/api/Test/Search/textToSearcFor

My routes are like those in this post

            config.Routes.MapHttpRoute(
                  name: "ApiById",
                  routeTemplate: "api/{controller}/{id}",
                  defaults: new { id = RouteParameter.Optional },
                  constraints: new { id = @"^[0-9]+$" }
              );

        config.Routes.MapHttpRoute(
            name: "ApiByName",
            routeTemplate: "api/{controller}/{action}/{name}",
            defaults: null
        );

        config.Routes.MapHttpRoute(
            name: "ApiByAction",
            routeTemplate: "api/{controller}/{action}",
            defaults: new { action = "Get" }
        );

What am I doing wrong?

Upvotes: 1

Views: 284

Answers (1)

mechenbier
mechenbier

Reputation: 3067

For your "ApiByName" route, Try changing

routeTemplate: "api/{controller}/{action}/{name}"

to

routeTemplate: "api/{controller}/{action}/{text}",

Or

Change the parameter name "text" of your Search action to "name"

Upvotes: 3

Related Questions