Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "No type was found that matches the controller named 'SampleSlashBaseService'" when trying to use WebAPI

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.

like image 747
azzlack Avatar asked Jul 08 '12 15:07

azzlack


3 Answers

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

like image 94
azzlack Avatar answered Nov 01 '22 03:11

azzlack


You also need to make sure the Controller class is Public

like image 11
Eranga Avatar answered Nov 01 '22 04:11

Eranga


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

like image 6
mcanti Avatar answered Nov 01 '22 03:11

mcanti