0

I am trying to implement three methods controller

public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public IEnumerable<string> Get(int id)
        {
            return new string[] { "sa1", "sa2" };
        }
        [HttpGet]
        public IEnumerable<string> password()
        {
            return new string[] { "password", "password" };
        }
 config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

But When i try to call http://localhost:49365/api/mycontrollername/password

it's always showing The request is invalid.

  • Can you show us your routing config. Also what is the exact error you are getting? (i.e. 404 not found). – Jon Susiak May 09 '14 at 07:38
  • No it worked i have replace default path withconfig.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); – Saurabhchauhan232 May 09 '14 at 08:57

2 Answers2

0

I suspect it is attempting to call Get(int id) and then trying to pass the word "password" as the integer parameter. From what I can recall it's down to convention-over-configuration in that when you make a GET request it looks for a method named Get. In this case it finds one. Then based on the routing, i.e. after the controller name comes an ID, it looks at the next part of the URL, in this case "password", and then uses that as a value for the id argument.

If you were to remove the two Get methods you probably find that your URL works, but if you add other HttpGet methods you will run into other issues related to "multiple actions found". The following discussion may help if you decide you need to have multiple HttpGet methods:

Web Api Routing for multiple Get methods in ASP.NET MVC 4

Community
  • 1
  • 1
MotoSV
  • 2,348
  • 17
  • 27
0

if you want to call an api function like "http://localhost:49365/api/mycontrollername/password"

you have to add ActionName attribute on the controller and add route on the Routeconfig.cs;

here is the example

    [ActionName("GetEmployees")]
    public IEnumerable<Employee> GETEmployees()
    {
        return _db.Employees.AsNoTracking();
    }

   routes.MapRoute(
            name: "Default",
            url: "api/{controller}/{action}/{id}",
            defaults: new { controller = "Employees", action = "GetEmployees", id =     UrlParameter.Optional }
        );
eric
  • 41
  • 5