0

it might have duplicate but i didn't find right solution,

My web api,

public class SampleController : ApiController
{
    public string Get(int id)
    {
        return "value";
    }

    public string hello(int id)
    {
        return "value";
    }
} 

my webapiconfig,

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

my problem is

When i call http://localhost:1234/api/Sample/5 it's hitting Get(int id) but how can i call method 2 i.e hello(int id) ?? what needs to be changed and what's the best way to handle these kind of scenarios ??

6
  • Do you want to call hello method or Gcccet Commented Feb 26, 2018 at 13:00
  • To call hello method... localhost:1234/api/Sample/hello/5 ... Commented Feb 26, 2018 at 13:01
  • @Mittal, hello(int id) not hitting with localhost:1234/api/Sample/hello/5 Commented Feb 26, 2018 at 13:04
  • Change the routeTemplate: "api/{controller}/{action}/{id}" Commented Feb 26, 2018 at 13:06
  • 1
    Okay, so if a GET to http://localhost:1234/api/Sample/5 is meant to hit hello, how is Get meant to be accessed? Commented Feb 26, 2018 at 13:20

2 Answers 2

1

TLDR:

If you want to reference individual actions in your Web API then change your routing to this:

routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

Then you can access your action like this: localhost/api/{controller}/{action}/. Look here for further information, especially "Routing by Action Name".

Orig:

You seem to expect the same behaviour as with MVC Controllers. The Standard-Routing for MVC-Controller is this:

routeTemplate: "{controller}/{action}/{id}"

This corresponds to the name of the controller, the method which is to be used and some form of input. ApiControllers Route differently:

routeTemplate: "staticPart/{controller}/{id}"

As you can see there is only a reference to the individual controller and the input, as well as the "staticPart" which normally is something like /api/

The Idea is that you use a RESTful approach, connecting methods with different types of http methods (eg. DELETE, GET, POST, PUSH and PUT)

The Get Method in your example is a special because through the name "Get" you have told the compiler that this method corresponds with HTTP-GET.

So to get to your question: Either you change your Routing to that of MVC-Controller. So that you reference individual actions in your requests or you use different HTTP-Methods. Or you set routes indivdually as shown by MaxB

You can find an official overview on Web API routing here There you'll find examples on all possibilities.

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

Comments

0

I'm not sure if I got your problem right, but if I did:

You should specify the route for the function not in its name, but in some other way. From my little experience with the topic, that's how I do it:

[HttpGet]
[Route("SystemInfo")] // That's the name of the route you will call
public IHttpActionResult SystemInfo()
{
    return Ok();
}

Consider checking this out.

So, considering your question, it would be like so:

[Route("Get")]
public string Get(int id)
{
    return "value";
}

[Route("hello")]
public string hello(int id)
{
    return "value";
}

1 Comment

so what would be the urls for consumption of both methods ?

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.