简体   繁体   中英

WebApi Controller no action was found for the controller

A real head-scrather this one. I have created two ApiControllers which I am using as a JSON webservice:-

namespace ControlTower.Controllers
{
    public class AirlinesController : ApiController
    {
        private static IEnumerable<Airline> MapAirlines()
        {
            return (Jetstream.AirlineObject.GetAirlines()).Select(x => x);
        }

        [HttpGet]
        public IEnumerable<Airline> GetAirlines()
        {
            return MapAirlines().AsEnumerable();
        }

        [HttpGet]
        public Airline GetAirlineByCode(string code)
        {
            return Jetstream.AirlineObject.GetAirline(code);
        }
    }
}

and:-

namespace ControlTower.Controllers
{
    public class ReviewsController : ApiController
    {
        private static IEnumerable<Review> MapReviews(int airline)
        {
            return (Jetstream.ReviewObject.GetReviews(airline)).Select(x => x);
        }

        [HttpGet]
        public IEnumerable<Review> GetReviews(int airline)
        {
            return MapReviews(airline).AsEnumerable();
        }

        [HttpGet]
        public Review GetReviewById(int review)
        {
            return Jetstream.ReviewObject.GetReview(review);
        }
    }
}

With this routing:-

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

And whilst visiting /api/airline/get/ba or /api/airline/get/ works perfectly, visiting any variation of Review does not. Can anyone see anything really obvious I'm missing here?

Help is appreciated.

Your default route is expecting a parameter named "code". You either need to add a route to accept a parameter named airline and/or review, or explicitly tell the controller the name of the parameter.

ex. /api/reviews/get?airline=1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM