20

I have a webapi project with a base ApiController named SlashBaseService:

[RouteArea("uBase")]
public abstract class SlashBaseService : ApiController
{
}

The resulting dll is used in a WebForms project so I also have a WebActivator class with the following code to generate routes:

RouteTable.Routes.MapHttpAttributeRoutes(config =>
{
    // Get all services inheriting from SlashBaseService
    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        foreach (var type in assembly.GetTypes())
        {
            if (type.IsSubclassOf(typeof(SlashBaseService)))
            {
                // Scan assembly
                config.ScanAssembly(assembly);

                // Skip the remaining types in this assembly
                break;
            }
        }
    }
});

RouteTable.Routes.MapHttpRoute(
    name: "DefaultBase",
    routeTemplate: "base/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional });

I also have a testservice in a separate assembly:

public class SampleSlashBaseService : SlashBaseService
{
    [GET("TestOpenMethod")]
    public string GetTestOpenMethod()
    {
        return "Hello anonymous!";
    }

    [GET("Echo/{message}")]
    public string GetEcho(string message)
    {
        return message;
    }
}

All pretty simple stuff. The problem is when I try to go to one of the urls this generates i get the following message:

No type was found that matches the controller named 'SampleSlashBaseService'.

The route list from /routes.axd also looks correct.

Charles
  • 50,943
  • 13
  • 104
  • 142
azzlack
  • 1,191
  • 1
  • 9
  • 28

5 Answers5

54

Found the problem.

ApiControllers class names need to be suffixed with "Controller", and mine was not. Changing it to SampleSlashBaseController solved the problem.

NOTE: It is possible to suffix it with "Service" as I did, but then you have to implement a custom IHttpControllerSelector like described here: http://netmvc.blogspot.no/2012/06/aspnet-mvc-4-webapi-support-areas-in.html

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
azzlack
  • 1,191
  • 1
  • 9
  • 28
  • 7
    I did something along the opposite lines. I had Controller where it wasn't supposed to be! `defaults: new { controller = "APIController" }` should be `defaults: new { controller = "API" }` You must OMIT `Controller` in your `WebApiConfig.cs`! – skovy Jul 11 '14 at 15:57
  • That's what happened to me where I left the Controller suffix on the WebApiConfig.cs. Your comment helped me :) – Dr Schizo Jan 21 '15 at 13:08
  • 1
    I was pulling my hair out before I found this. What a silly error to make. THANK YOU – George Harnwell May 06 '15 at 09:19
  • Stupid as it might sound: Make sure that you suffix it with `Controller`, not `Controlller`! Just spent 30' wondering what on earth is going on... – Peter Albert Jul 23 '15 at 08:26
  • i really wish that frameworks didn't make stupid opaque requirements about how classes are named – Zach Smith Sep 08 '17 at 16:54
  • you saved my day! :P – Kas Oct 20 '17 at 07:57
11

You also need to make sure the Controller class is Public

Eranga
  • 161
  • 1
  • 2
  • 1
    this question is already answered however my comment was the answer for the issue I had. I had the same error however I was missing Public. I wanted to leave this here in case it would help someone else. – Eranga Dec 09 '15 at 02:11
6

In my case the Controller was defined properly, but was not marked public.

mcanti
  • 1,964
  • 2
  • 17
  • 14
2

In my case, there were two controllers with the same name, in different folders. Renaming them solved the problem instantly.

regisxp
  • 956
  • 2
  • 10
  • 31
2

For what it's worth, I ran into this and my problem was my manually-added class wasn't inheriting from ApiController. The class definition needs to be public class QueueController : ApiController not public class QueueController. What a dumb thing to overlook.

Adam S
  • 121
  • 3
  • Thank you! This helped me. I was inheriting from System.Web.Mvc.Controller, but I wanted to be inheriting from System.Web.Http.ApiController. – dpberry178 Mar 18 '21 at 20:29