user517406
user517406

Reputation: 13773

Calling custom method from API controller

I want to create a custom method in my Web API controller, so rather than just returning all data I can pass in a parameter e.g my controller is called StandingController and I want to create a method called GetStandingsBySeason(string season). I have changed my WebApiConfig to to look like this :

config.Routes.MapHttpRoute(
            "DefaultApi",
            "api/{controller}/{id}",
            new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            "DefaultApiWithAction",
            "api/{controller}/{action}/{id}",
            new { id = RouteParameter.Optional }
        );

And here is the method in my controller :

public IEnumerable<StandingDTO> GetStandingsBySeason(string season)
    {
        return _repository.GetStandingsBySeason(season).Select(Mapper.Map<StandingDTO>);
    }

And in my front end I am calling it like this :

$.getJSON("/api/standing/GetStandingsBySeason/2011", self.standings);

But the method never gets called, can anybody shed any light on what I am doing wrong with this?

Upvotes: 1

Views: 2122

Answers (1)

Eugene
Eugene

Reputation: 1535

Looks like your configuration is wrong: your template is

"api/{controller}/{action}/{id}"

but the method gets the parameter called season, so you can call it like in the following way:

$.getJSON("/api/standing/GetStandingsBySeason?season=2011", self.standings);

Or you can modify you route template to

"api/{controller}/{action}/{season}"

Upvotes: 2

Related Questions