1

I am trying to create a route that will be handled by each of my controller's GetAll method vs. the paged, default Get method so that the url looks like the following: "api/{controller}/all"

    public class MyController {
        public IEnumerable<MyModel> GetAll() {
            ...
        }
        public IEnumerable<MyModel> Get(int page = 0, int pageSize = 50) {
            ...
        }
    }

    public class MyOtherController {
        public IEnumerable<MyOtherModel> GetAll() {
            ...
        }
        public IEnumerable<MyOtherModel> Get(int page = 0, int pageSize = 50) {
            ...
        }
    }

My routes looks like this currently:

    config.Routes.MapHttpRoute(
        name: ControllerOnlyRoute,
        routeTemplate: "api/{controller}"
    );

    config.Routes.MapHttpRoute(
        name: ControllerAndIdRoute,
        routeTemplate: "api/{controller}/{id}",
        defaults: null
    );

    config.Routes.MapHttpRoute(
        name: ControllerActionRoute,
        routeTemplate: "api/{controller}/{action}"
    );

I'm not sure how to create a route for each controller that will route to the GetAll method?

Flea
  • 1,490
  • 2
  • 19
  • 43

1 Answers1

1

You should use the Attribute routing nuget package for this. It makes setting up your routing a lot easier and even comes as part of WebApi 2. The documentation for it can be found here.

James
  • 2,195
  • 1
  • 19
  • 22
  • As an alternative, this solution also works (and was what I ultimately went with rather than Attribute Routing): http://stackoverflow.com/questions/9499794/single-controller-with-multiple-get-methods-in-asp-net-web-api – Flea Jan 03 '14 at 17:28