1

Right now, my API controller has 2 methods: one to get ALL events, and one to get ONE event.

namespace HobbsEventsMobile.Controllers
{
    public class EventController : ApiController
    {
        // GET api/event
        [HttpGet]
        public List<HobbsEventsMobile.Models.Event> Get()
        {
            return HobbsEventsMobile.Models.Event.GetEventSummary();
        }

        // GET api/event/5
        [HttpGet]
        public HobbsEventsMobile.Models.Event Get(int id)
        {
            return HobbsEventsMobile.Models.Event.GetEventDetails(id);
        }
    }
}

Newly requested functionality requires me to add a way to call events for the current week. I have a stored proc and a method to call this, but I am not sure how to specify the URL. I would like to add this:

[HttpGet]
public List<HobbsEventsMobile.Models.Event> Get()
{
    return HobbsEventsMobile.Models.Event.GetThisWeeksEvents();
}

but make it accessible at m.mydomain.com/api/event/thisweek (or something). How do I do that?

2
  • Have you tried attribute routing? Commented Jun 18, 2014 at 19:43
  • @jasonp I'm not sure what that is. So.... Probably not? Can you elaborate. Commented Jun 18, 2014 at 19:45

1 Answer 1

2

You have two different options depending on what version of ASP.NET Web API you're running. If you're on version one you can simply follow the convention based routing and use:

public class EventController : ApiController
{
    [HttpGet]
    public List<HobbsEventsMobile.Models.Event> ThisWeek()
    {
        return HobbsEventsMobile.Models.Event.GetThisWeeksEvents();
    }
}

You will also need to modify your route definitions to support an action name (by default the framework picks the method based upon the HTTP verb):

config.Routes.MapHttpRoute(
    "DefaultApiWithId",
    "api/{controller}/{id}",
    new { id = RouteParameter.Optional }, new { id = @"\d+" }
);

config.Routes.MapHttpRoute(
    "DefaultApiWithAction",
    "api/{controller}/{action}"
);

config.Routes.MapHttpRoute(
    "DefaultApiGet",
    "api/{controller}",
    new { action = "Get" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

If you're using version two, you can still use the convention based routing, but you also have the ability to use attribute routing:

public class EventController : ApiController
{
    [HttpGet]
    [Route("event/thisweek")]
    public List<HobbsEventsMobile.Models.Event> ICanNameThisWhateverIWant()
    {
        return HobbsEventsMobile.Models.Event.GetThisWeeksEvents();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am using version one, and implemented this as reflected in your first example. However, now I get an error when making an api call to /event: Multiple actions were found that match the request: System.Collections.Generic.List1[HobbsEventsMobile.Models.Event] Get() on type HobbsEventsMobile.Controllers.EventController` System.Collections.Generic.List1[HobbsEventsMobile.Models.Event] ThisWeek() on type HobbsEventsMobile.Controllers.EventController` any ideas?
@anwyatt I updated my answer with the correct routing modification. I was thinking MVC style when the routes for Web API are a bit different.

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.