Multiple actions were found that match the request Web API?

后端 未结 8 1179
再見小時候
再見小時候 2020-12-09 08:09

I am using web API and i am new in this. I am stuck in a routing problem. I have a controller with following actions :

    // GET api/Ceremony
    public IEn         


        
相关标签:
8条回答
  • 2020-12-09 08:49

    Edit Your WebApiConfig.cs in App_Start folder on the root of project and add {action} to routeTemplate parameter in MapHttpRoute Method like below :

     config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    
    0 讨论(0)
  • 2020-12-09 08:53

    Please check you have two methods which has the different name and same parameters.

    If so please delete any of the method and try.

    This error was raised because there are two methods which are looking for same parameters. try to delete any one of them and try...

    0 讨论(0)
  • 2020-12-09 08:54

    The problem here is your 2 Get methods will resolve to api/Ceremony and MVC does not allow parameter overloading. A quick workaround (not necessarily the preferred approach) for this sort of problem is to make your id parameter nullable e.g.

    // GET api/Ceremony
    public IEnumerable<Ceremony> GetCeremonies(int? id)
    {
        if (id.HasValue)
        {
            Ceremony ceremony = db.Ceremonies.Find(id);
            return ceremony;
        }
        else
        {
            return db.Ceremonies.AsEnumerable();
        }
    }
    

    However, you would then be returning a list of ceremonies when with 1 item when your trying to query for a single ceremony - if you could live with that then it may be the solution for you.

    The recommended solution is to map your paths appropriately to the correct actions e.g.

    context.Routes.MapHttpRoute(
        name: "GetAllCeremonies",
        routeTemplate: "api/{controller}",
        defaults: new { action = "GetCeremonies" }
    );
    
    context.Routes.MapHttpRoute(
        name: "GetSingleCeremony",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { action = "GetCeremony", id = UrlParameter.Optional }
    );
    
    0 讨论(0)
  • 2020-12-09 08:55

    I found another fix that doesn't require moving methods out of the controller or changing the route mapping config. Just add the [NonAction] attribute to the method you want to exclude:

    [NonAction]
    public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
    
    0 讨论(0)
  • 2020-12-09 08:59

    Here is my controller:

    public class PhoneFeaturesController : ApiController
    {
        public List<PhoneFeature> GetbyPhoneId(int id)
        {
            var repository = new PhoneFeatureRepository();
            return repository.GetFeaturesByPhoneId(id);
        }
    
        public PhoneFeature GetByFeatureId(int id)
        {
            var repository = new PhoneFeatureRepository();
            return repository.GetFeaturesById(id);
        }        
    }
    

    Here is my api routing:

            config.Routes.MapHttpRoute(
                name: "ApiWithId", 
                routeTemplate: "Api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional },
                constraints: new { id = @"^[0-9]+$" });
    
            config.Routes.MapHttpRoute(
                name: "ApiWithAction", 
                routeTemplate: "api/{controller}/{action}/{name}",
                defaults: null,
                constraints: new { name = @"^[a-z]+$" });
    
            config.Routes.MapHttpRoute(
                name: "ApiByAction",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { action = "Get" },
                constraints: new { id = @"^[0-9]+$" });
    
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    

    I tested it like this:

    /api/PhoneFeatures//api/PhoneFeatures/GetFeatureById/101
    
    /api/PhoneFeatures/GetByFeatureId/12
    

    It works smooth in every condition :)

    0 讨论(0)
  • 2020-12-09 09:03

    If you're not requirement bound to use REST services that use api/{controller}/{id} route and attempt to resolve action based on method GET/POST/DELETE/PUT, you can modify your route to classic MVC route to api/{controller}/{action}/{id} and it will solve your problems.

    0 讨论(0)
提交回复
热议问题