Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 716
dooburt Avatar asked Sep 01 '12 16:09

dooburt


People also ask

What is an action in Webapi?

In ASP.NET Web API, a controller is a class that handles HTTP requests. The public methods of the controller are called action methods or simply actions. When the Web API framework receives a request, it routes the request to an action. To determine which action to invoke, the framework uses a routing table.

How do I find my controller name in Web API?

Look in the route dictionary for the key "controller". Take the value for this key and append the string "Controller" to get the controller type name. Look for a Web API controller with this type name.

What is controller in Web API?

In Web API, a controller is an object that handles HTTP requests.


1 Answers

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

like image 144
Jason Bouchard Avatar answered Nov 07 '22 19:11

Jason Bouchard