1

I'm trying to create some web APIs in a new class library outside of my main website project (still in the same solution). In this library I've added the following API Controller:

namespace My.Custom.Namespace
{
    [ServicesController("mycustomnamespace.customapi")]
    public class CustomApi : ServicesApiController
    {
        public HttpResponseMessage TestMethod()
        {
            return Request.CreateResponse(HttpStatusCode.Accepted);
        }
    }
}

I then add this class library as a reference to my main website project, and try to call the API on this URL:

http://localhost/sitecore/api/ssc/mycustomnamespace/customapi/1/TestMethod

But I'm only receiving a 404: No HTTP resource was found that matches the request URI

How can I get a ServicesApiController to work from a referenced project/DLL?

Sitecore 8.2 Update 5

  • Why you have /1/ in url? Try removing this and renaming your controller like this "CustomApiController". You are missing the convention to have Controller in the end of name – Peter Procházka Jun 29 '18 at 11:39
  • @PeterProchazka Because the specification calls for it. Default routing is /sitecore/api/ssc/{namespace}/{controller}/{id}/{action} according to agehrke.com/2015/08/web-api-controller-in-sitecore-8 I've created and called WebAPIs like this, including the id parameter, from the website project successfully before, but when I follow the same procedure (basically the code in my question) while having the WebAPI in a referenced project/DLL, it doesn't work any more. – GTHvidsten Jun 29 '18 at 11:52
  • Can you add string id as parameter of your TestMethod and rename CustomApi to CustomApiController? – Peter Procházka Jun 29 '18 at 12:07
  • 1
    The disadvantage of that approach is that I have to implement pipelines and config files. I'd much rather use the other approach where things work "out of the box". I haven't tried your other suggestions yet, but will come back to you with results when I have. – GTHvidsten Jun 29 '18 at 12:32
  • 1
    @PeterProchazka It seems to be the naming convention that got me this time. I renamed my class to CustomApiController, and everything started working just fine. Thanks for the point in the right direction, and feel free to add this as an answer and I'll accept it. – GTHvidsten Jun 29 '18 at 14:32
2

You should follow MVC naming conventions for Controllers and rename your controller from CustomApi to CustomApiController.

Happy I could help you and that this did the trick.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.