locked
multiple actions were found that match the request RRS feed

  • Question

  • User-1177023326 posted

    Goodday All

    I am using web API and  am quite bit new in this. I  ve a trouble in a routing problem. I have a controller with following actions

    public class IndividualController : ApiController
       {
           static readonly IIndividualRepository repository = new IndividualRepository();
     
           [AllowAnonymous]
     
           [ResponseType(typeof(Individual))]
     
           [HttpGet]
           public IEnumerable<Individual> GetIndividual(string Ind)//
           {
     
              return repository.Get(Ind);                      
              
     
           }
     
     
           [HttpGet]
           public IEnumerable<Individual> GetTCC(string TCCNo)//
           {
     
               return repository.GetTCC(TCCNo);
     
     
           }
     
     
           [HttpGet]
           public IEnumerable<Individual> GetAllIndividual()
           {
               return repository.GetAll();
           }
    
    }


    public static class WebApiConfig
      {
          public static void Register(HttpConfiguration config)
          {
              // Web API configuration and services
     
              // Web API routes
              config.MapHttpAttributeRoutes();
     
              //  config.Routes.MapHttpRoute(
              //     name: "WithActionApi",
              //     routeTemplate: "api/{controller}/{action}/{Ind}"
              // );
     
              //  config.Routes.MapHttpRoute(
              //    name: "WithActionApi2",
              //    routeTemplate: "api/{controller}/{action}/{TCCNo}"
              //);
        
     
              config.Routes.MapHttpRoute("DefaultApiWithId""Api/{controller}/{Ind}"new { Ind = RouteParameter.Optional }, new { Ind = @"\d+" });
              config.Routes.MapHttpRoute("DefaultApiWithAction""Api/{controller}/{action}");
              config.Routes.MapHttpRoute("DefaultApiGet""Api/{controller}"new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
              config.Routes.MapHttpRoute("DefaultApiGet1""Api/{controller}"new { action = "GetTCC" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
              config.Routes.MapHttpRoute("DefaultApiPost""Api/{controller}"new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });
     
              // config.Formatters.XmlFormatter.
              config.Formatters.JsonFormatter.
              SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
          }
      }


    The problem occur when i added this action  GetTCC given this error Multiple actions were found that match request.
    Please can any help to solve this problem. It actually works when GetTCC was not included in the controller with the commented section of the routes used.
    But the point now is that i needed to have this two Method Get and GetTCC in a single controller.

    Thank You
    Thursday, September 15, 2016 10:43 AM

Answers

  • User36583972 posted

    Hi moruff,

    The problem occur when i added this action  GetTCC given this error Multiple actions were found that match request.

    Because, if you add  action GetTCC, you will have two same get actions. So, you will get the error. You can add Route Attributes to define your get actions to be different.

          //•http://localhost/Individual/GetIndividual/001
            [AllowAnonymous]
            [ResponseType(typeof(Individual))]
            [HttpGet]
            [Route("Individual/GetIndividual/{Ind}")]
            public IEnumerable<Individual> GetIndividual(string Ind)//
            {
                return repository.Get(Ind);
            }
    
            //•http://localhost/Individual/GetTCC/001
            [HttpGet]
            [Route("Individual/GetTCC/{TCCNo}")]
            public IEnumerable<Individual> GetTCC(string TCCNo)//
            {
                return repository.GetTCC(TCCNo);
            }

    Attribute Routing in ASP.NET Web API 2:

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 16, 2016 2:39 AM