1

I'm building my first app using web api, and I've stumbled upon a problem that I can not find information on.

Using sample controller to test if its working:

public class TestController : ApiController
{
    // GET api/test
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

WebApiConfig.cs:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

Every time I try to call the service using http://localhost:59502/api/test I am getting

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

Can someone point out what am I doing wrong?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Savio
  • 33
  • 1
  • 9
  • If you remove `config.MapHttpAttributeRoutes()` does it work? I might be wrong but if you use that option I think you also need to use the attributes on the routes e.g. `[Route("api/test/{id}")]` – James Nov 06 '14 at 16:50
  • Just tried - still no go. :( – Savio Nov 06 '14 at 16:52
  • 1
    @Savio, why would `"test"` route to "`Get`"? – Kirk Woll Nov 06 '14 at 16:53
  • @KirkWoll When you create an WebApi project (using an template) from VS2013, it works, the controller has an Get method, that returns 2 values, and you can call it using `http://localhost:52192/api/values`. The routing looks the same on both mine and templated project. – Savio Nov 06 '14 at 16:59
  • I just tried your code in a WebAPI project I'm working on and it worked fine. Are you configuring routes anywhere else? – Mark Nov 06 '14 at 17:06
  • @Mark No, I don't think I do. I have an basic routing set up for MVC (in RouteConfig.cs). Nothing else. – Savio Nov 06 '14 at 17:09
  • You have both a `RouteConfig.cs` and a `WebApiConfig.cs` in the same project? – Mark Nov 06 '14 at 17:11
  • @Mark Yes, while creating an Project I did select MVC and WebApi, so VS created both of them. – Savio Nov 06 '14 at 17:17
  • Just tried that as well and it works. What's your `RouteConfig.cs` and `Global.asax` look like? Did you change anything there? – Mark Nov 06 '14 at 17:21
  • @Mark Nothing added in those. Only what the template added. – Savio Nov 06 '14 at 17:27

4 Answers4

1

To determine which action to invoke, the framework uses a routing table. The Visual Studio project template for Web API creates a default route:

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

With the default routing template, Web API uses the HTTP method to select the action. However, you can also create a route where the action name is included in the URI:

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

This way you will be able to access your url ........./api/test

lordpiti
  • 11
  • 1
  • Added additional routing, no go. I think the problem is something different than not being unable to determine action to take, the error is about not finding an controller to start with, I understand it as it's not yet at the point of choosing action. – Savio Nov 06 '14 at 17:05
  • does it work if you try to access http://localhost:52192/api/test/Get/ (after changing the routing as i suggested)? Because for me it works perfectly – lordpiti Nov 07 '14 at 10:20
0

I have actually resolved the issue now - by creating new project, and copying all the classes over. Now everything works perfectly.

"Have you tried turning it off and on again?" Oh well..

Savio
  • 33
  • 1
  • 9
  • Don't want to be mean here, but I honestly don't think that is a real answer. What was the issue? I am facing the same problem and I am looking to solve it. – SoftwareSavant Sep 11 '15 at 12:15
  • I'd leave an fully detailed answer only if I had one - I did exactly what I wrote. Recreating the project and moving all classes over resolved the issue for me. – Savio Sep 15 '15 at 11:32
0

For me the problem was due to renaming the project assembly. After renaming and rebuilding project, the old assembly still remained in output (bin) folder and it was somehow conflicting with the new one.

Viktors Telle
  • 763
  • 9
  • 22
0

My issue was that I didn't add the "Controller" suffix to the name of the file (duplicated another controller and renamed it)

Just add Controller to the end of the file name and the class name.

mz7xd
  • 19
  • 4