1

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?

1 Answer 1

2

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}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.