4

UPDATE: I unloaded the project and re-did it again and it worked.

I'm trying to create a WebApi, my build works fine and I get the error message "No type was found that matches the controller named" when I try to go to URI

This is how my webapiconfig looks like,

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

And below is my controller,

public class ClientController : ApiController
{

    public List<Client> Get()
    {
        ICRepository repository = new CRepository(new CContext());

        return repository.GetAllClients().ToList();
    }

And this is how my global.asax.cs file looks like,

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
}

I'm trying to browse to the url "http://localhost:50662/api/client"

The complete error is as below,

This XML file does not appear to have any style information associated with it. The document tree is shown below. No HTTP resource was found that matches the request URI 'http://localhost:50662/api/Client'.No type was found that matches the controller named 'Client'.

My question is different from what it's been marked as duplicate of, the question uses just controller which is MVC and mine is "ApiController" and I did read that before creating this. Also, the marked answer there is similar to what I have in here and my problem still exists.

Any help would be highly appreciated. I'm really lost.

user1345260
  • 2,151
  • 12
  • 33
  • 42

4 Answers4

2

I was getting the same error:

Error><Message>No HTTP resource was found that matches the request URI 'http://localhost:53569/api/values'.</Message><MessageDetail>No type was found that matches the controller named 'values'.</MessageDetail></Error>

By default when I created new controller in asp.net web forms application, it was like this:

ValuesController1 : ApiController

I just simply removed "1", and make it:

ValuesController : ApiController

I don't know if it is a bug or whatever, but it works for me.

M_Idrees
  • 2,080
  • 2
  • 23
  • 53
0

you are calling WebApiConfig twice.

 WebApiConfig.Register(GlobalConfiguration.Configuration);
 GlobalConfiguration.Configure(WebApiConfig.Register);

Remove one of them. if you are using Web Api Version >= 2.0, then remove the first one, otherwise remove the second one.

Edit 1

I think your issue may be similar to this Answer

You might be missing some reference libraries, may not be directly related to Your Controller or API, but the way Web Api finds the Controller, is it go through all assemblies referenced by the application and search for types matching a predicate.

Community
  • 1
  • 1
pjobs
  • 1,247
  • 12
  • 14
  • I have done that as well. I will update my post. But it still doesn't work – user1345260 May 06 '15 at 16:50
  • where do you have ClientController.cs file? can you make sure in properties set to Build Action settings to Compile for this file. – pjobs May 06 '15 at 16:57
  • I removed that but still the same error. The file is within the Controllers folder and the BuildAction is set to compile – user1345260 May 06 '15 at 17:06
0

Although it isn't the cause of your problem, mine was actually caused by adding a new item of type class through Visual Studios context-menu and not a Controller.

The generated file had the following structure:

class foo
{
}

I extended the class to inherit from ApiController, but forgot to mark it as public

This is the correct version:

public class FooController : ApiController
{
    public string Get(string name="Bar")
    {
        return $"Hello {name}"};
    }
}
Marco
  • 22,856
  • 9
  • 75
  • 124
0

Right now I am facing a very similar issue. My error message was No type was found that matches the controller named 'odata'. It seems really strange to me, that it seems to be searching for a controller named ODataController or something like that. So I started looking for the real issue. In my case, it was the fact, that I had forgotten to include a public parameterless contructor to the controller class. I added it and the error went away immediately.