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 }


            );  

2 Answers 2

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"?

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

Comments

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

6 Comments

i make changes but no Results Same Error
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
i make changes in Edit
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.
i did change also Even no Result...Wil u come on my Teamviewr
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.