Copied to clipboard

Flag this post as spam?

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


  • Graeme 19 posts 110 karma points
    Jan 22, 2015 @ 15:52
    Graeme
    0

    UmbracoApiController - default route??

    Hi I'm trying to create an UmbracoApiController and from what I've read the default route should be umbraco/api/{controller}/{action}/{id}

    My code is from a project and gets copied into the sites/bin folder.

    I have the following 

    namespace My.Controllers
    {
        using System.Net;
        using System.Net.Http;    
        using Umbraco.Web.WebApi;
    
        public class TestApiController : UmbracoApiController
        {
            public HttpResponseMessage Index() {           
    
                var response = this.Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StringContent("why is this not working??");
                return response;
            }
        }
    }

    But when I try a request from https://mysite.com/umbraco/api/testapi/index

    The page displays the following 

    No HTTP resource was found that matches the request URI 'https://mysite.com/umbraco/api/testapi/index'.
    No type was found that matches the controller named 'TestApi'.

    The default routing for Surface controllers works fine from the same project? 
    Any ideas where I'm going wrong??
    Thanks

  • Richard Terris 273 posts 714 karma points
    Jan 22, 2015 @ 17:08
    Richard Terris
    0

    Hi Graeme,

    I'm not sure if this will make a difference, but I think since your method is named Index, you might not need the method name on the URL, so https://mysite.com/umbraco/api/testapi/

    Also, I'd try decorating the method with the [RequireHttps] attribute?

    I noticed your URL is https which I'm not sure if that has something to do with your issue

    Richard

  • Graeme 19 posts 110 karma points
    Jan 22, 2015 @ 17:35
    Graeme
    0

    Hi Richard,

    Without the action name (Index) I get a 404, I've also decorated the action with [RequireHttps] attribute but I still get the same result

  • Richard Terris 273 posts 714 karma points
    Jan 22, 2015 @ 17:44
    Richard Terris
    0

    Hi Graeme,

    My only other suggestion is that perhaps having the using statements inside your namespace may be causing compilation issues?

    Try moving them to before the namespace. See this article: http://www.hanselman.com/blog/BackToBasicsDoNamespaceUsingDirectivesAffectAssemblyLoading.aspx

  • Graeme 19 posts 110 karma points
    Jan 22, 2015 @ 18:01
    Graeme
    0

    Nope no joy moving the using references to before the namespace, having the usings within the namespace works fine for any suface controllers I have within the project.

    Thanks anyway.

  • Richard Terris 273 posts 714 karma points
    Jan 22, 2015 @ 18:02
    Richard Terris
    0

    Actually,

    I've read some more on that and it won't make any difference at all.

    It's more likely related to the routing.

    take a look in the App_Start folder and in the WebApiConfig file - is the default route expecting an id?

    If so, I'd add a new route ABOVE this default one, without any parameter.

    Richard

  • Graeme 19 posts 110 karma points
    Jan 22, 2015 @ 18:08
    Graeme
    0

    I've also tried adding my own custom route

    protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        RouteTable.Routes.MapHttpRoute(
            "MyTestApi",
            "MyTestApi/{controller}/{action}/{id}"new { id = UrlParameter.Optional });     
    }

    request : https://mysite.com/mytestapi/testapi/index

    still gets the error message 

    <Error>
    <Message>
    No HTTP resource was found that matches the request URI 'https://mysite.com/mytestapi/testapi/index'.
    </Message>
    <MessageDetail>
    No type was found that matches the controller named 'testapi'.
    </MessageDetail>
    </Error>
  • Richard Terris 273 posts 714 karma points
    Jan 22, 2015 @ 18:20
    Richard Terris
    100

    try:

    public HttpResponseMessage Index(int? id) {           
    
            var response = this.Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StringContent("why is this not working??");
            return response;
        }
    
  • Graeme 19 posts 110 karma points
    Jan 22, 2015 @ 18:47
    Graeme
    0

    nope, I have a breakpoint on my action and its not even hitting the method. Must be the routing, but just can't see why?

    It is OK to use the UmbracoApiController in the same project as my main project isn't it? i.e. my main project is all my code behind for mysite (Models, Controllers (SurfaceControllers, etc) this project gets built then copied into the httpdocs/bin of my Umbraco site

  • Richard Terris 273 posts 714 karma points
    Jan 22, 2015 @ 19:18
    Richard Terris
    0

    Yeah that's fine, umbraco api controller inherits from apicontroller in .net anyway.

    I think you're right about the route. Try:

    routes.MapHttpRoute(
    name: "MyDefault",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
    

    );

  • Richard Terris 273 posts 714 karma points
    Jan 22, 2015 @ 19:18
    Richard Terris
    0
  • Graeme 19 posts 110 karma points
    Jan 23, 2015 @ 10:57
    Graeme
    0

    Nope still no luck, still getting the error when trying the custom route without the action

    No HTTP resource was found that matches the request URI 'https://mysite.com/mytestapi/testapi/'

    No type was found that matches the controller named 'testapi'

  • Richard Terris 273 posts 714 karma points
    Jan 23, 2015 @ 13:12
    Richard Terris
    0

    The URL should definitely be https://mysite.com/umbraco/api/testapi/index/

    It really sounds like the routing is the issue but I'm a bit lost as to what current state things are in.

    I think you the default webapi route should be fine as Umbraco web api inherits from standard .net api

    Can you post the current code for the route and for the method so we can start over?

  • Graeme 19 posts 110 karma points
    Jan 23, 2015 @ 13:51
    Graeme
    0

    Hi Richard,

    Just tried the default umbraco route again umbraco/api/testapi/index/ and it returned a different error

    The requested resource does not support http method 'GET'.

    It must have been since I'd added the nullable int param, since adding that I think I'd only been trying my custom route.

    I resolved the above error by decorating the action with [System.Web.Http.AcceptVerbs("GET")]

    need the FQN as AcceptVerbs clashed with the System.Web.Mvc reference.

    Thanks for your help and input.

     

    Graeme

  • Richard Terris 273 posts 714 karma points
    Jan 23, 2015 @ 14:28
    Richard Terris
    0

    Glad you got it sorted :)

Please Sign in or register to post replies

Write your reply to:

Draft