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.
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.
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.
In Web API, a controller is an object that handles HTTP requests.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With