nichu09
nichu09

Reputation: 892

web api routing not working for two Get method?

in my web api i tried to get 2 Get methods .one is with parameter and one is without parameter.

 public HttpResponseMessage Get()
    {}  
 public HttpResponseMessage GetAll(int id)
    {}

my routing like this

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id =  UrlParameter.Optional }

first get method is return value.but second get method with parameter is giving error.pls give some suggession.advance thanks

Upvotes: 1

Views: 1299

Answers (1)

What you have shown is for MVC. The Visual Studio project template for Web API creates some thing like this in WebApiConfig.cs under App_Start:

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

With that, a GET request on http://localhost:<port>/api/yourcontroller should call Get() and GET on http://localhost:<port>/api/yourcontroller/123 should call GetAll(int). See this for more info.

Upvotes: 1

Related Questions