0

Here I'm Trying to implement multiple Get methods but the browser througs an error Multiple actions were found that match the request. Why does this happen?

In the Api Controller I added two methods: 1. GetEmployee 2. HelloDept If I comment out one of them it's working fine.

    public class TrailController : ApiController
    {
        private IProduct Repo = new Product();

        [HttpGet]
        public IEnumerable<Employee> GetEmployee()

        {
            var x = Repo.GetEmp();
            return x;

        }
        [HttpGet]
        public IEnumerable<Department> HelloDept()

        {
            var x = Repo.GetDept();
            return x;

          }

RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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


            );  
jps
  • 20,041
  • 15
  • 75
  • 79

2 Answers2

1

Change your code in RouteConfig.cs.

From:

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


        );

To:

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

Also, refer this- Difference between "MapHttpRoute" and "MapRoute"?

Community
  • 1
  • 1
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
0

In Your WebApiConfig.cs change

`api/{controller}/{id}` 

with

`api/{controller}/{action}/{id}`.

Then call the actions like -

http://localhost:port/api/controller/HelloDept
http://localhost:port/api/controller/GetEmployee


public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

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

enter image description here

enter image description here

Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24
  • i make changes but no Results Same Error –  Jan 05 '17 at 07:41
  • Multiple actions were found that match the request: GetEmployee on type Application.Api.Controllers.TrailController GetById on type Application.Api.Controllers.TrailController HelloDept on type Application.Api.Controllers.TrailController –  Jan 05 '17 at 07:43
  • i make changes in Edit –  Jan 05 '17 at 07:49
  • it should be `api` and not `{api}`. one more thing, you're using `ApiController`, but what you posted is a `route` for `mvc`.You sure you looking at right place? It should be an `HttpRoute` in the `WebAPiConfig.cs`. – Amit Kumar Ghosh Jan 05 '17 at 07:52
  • i did change also Even no Result...Wil u come on my Teamviewr –  Jan 05 '17 at 07:54
  • I don't have teamviewer, dude sorrry. But as you can see both the actions are being called, I believe that you are putting your code in a wrong place. – Amit Kumar Ghosh Jan 05 '17 at 08:12