Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Nov 11, 2013 @ 09:50
    Simon Dingley
    0

    UmbracoApiController - No HTTP resource was found that matches the request URI

    I'm working on a custom API controller and when I have just a single action in there it resolves fine however adding a second it never resolves and I can't work out if it is a bug or I am missing something somewhere (with it being Monday morning and all!).

    public class ScheduleApiController : UmbracoApiController
    {
      // GET: /Umbraco/Api/ScheduleApi/Get
      public HttpResponseMessage Get()
      {
        ... // This resolves just fine
      }
    
      // GET: /Umbraco/Api/ScheduleApi/GetByDay/2
      public HttpResponseMessage GetByDay(int day)
      {
        ... // This won't resolve
      }
    }
    

    My second action simply returns the exception in the title of this post with the message detail of "No action was found on the controller 'ScheduleApi' that matches the request."

    Any ideas?

    Thanks, Simon

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 11, 2013 @ 10:41
    Jeavon Leopold
    0

    Hi Simon,

    Have you tried changing the first method to be called GetSomething(), I have a feeling that Get() does something special, but I might be wrong...

    Jeavon

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 11, 2013 @ 10:49
    Jeavon Leopold
    102

    Wait, I remember, try /Umbraco/Api/ScheduleApi/GetByDay/?day=2

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 11, 2013 @ 10:53
    Jeavon Leopold
    3

    I think only a parameter called id can be auto passed by url so to get this route to work /Umbraco/Api/ScheduleApi/GetByDay/2 you would need your method to be like: public HttpResponseMessage GetByDay(int id)

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Nov 11, 2013 @ 11:15
    Simon Dingley
    0

    Thanks Jeavon - looks like you are right on both counts there.

    /Umbraco/Api/ScheduleApi/GetByDay/?day=2
    

    This works, as does your second suggestion. Thanks for the pointers however it seems a bit odd don't you think that it's been implemented in this way? Whilst I would prefer to maintain a consistent structure for urls the method signature doesn't sit quite right with me referring to the day as the id.

    Simon

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 11, 2013 @ 12:26
    Jeavon Leopold
    0

    Hi Simon,

    Kinda, when you inherit from UmbracoApiController, your controller is auto routed so that you don't have to register your own route, however in native WebApi it is when you register a route that you can specify you own pattern. I don't know if this is possible with the UmbracoApi but I think it probably is....

    Jeavon

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 12, 2013 @ 10:53
    Sebastiaan Janssen
    0

    Just to chime in, this is absolutely default ASP.NET Web Api behavior, we're not doing anything different from the defaults. We can't smell that you're going to use "day" instead of "id". Of course you can always define your own routes which expect a param called day like:

    using System.Web.Http;
    using System.Web.Routing;
    using Umbraco.Core;
    
    namespace My.Namespace
    {
        public class StartupEventHandlers : ApplicationEventHandler
        {
            public StartupEventHandlers()
            {
                UmbracoApplicationBase.ApplicationStarting += UmbracoApplicationStarting;
            }
    
            static void UmbracoApplicationStarting(object sender, System.EventArgs e)
            {
                RouteTable.Routes.MapHttpRoute("DefaultApi", "my/api/{controller}/{day}", new { day = RouteParameter.Optional });
            }
        }
    }
    

    So your GET would be: // GET: /My/Api/ScheduleApi/GetByDay/2

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 12, 2013 @ 11:47
    Jeavon Leopold
    0

    Hey Seb,

    I thought I tried that yesterday but I think I had it in the wrong start up event or something as it works perfectly today!

    If you don't want to have a whole new route you can add something to this:

    RouteTable.Routes.MapHttpRoute("ScheduleApi", "Umbraco/Api/ScheduleApi/{action}/{day}", new { controller = "ScheduleApi", day = RouteParameter.Optional });
    

    Fairly useful actually :-)

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft