0

I'm using attribute routing to specify my routes in a controller that inherits from System.Web.Http.ApiController. I'm getting some funky behavior. Here's some sample code:

[RoutePrefix("api/1/user")]
public class UserRestController : ApiController
{
    UserService userService = new UserService();

    [Route("{id}"), HttpGet]
    public UserDTO Get(string id)
    {
        var user = userService.GetUserById(id);

        return user;
    }
}

That works absolutely how I would expect. When I visit ~/api/1/user/someId I get the expected information back. When I switch to not include the '1' in the route prefix I get "No HTTP resource was found that matches the request URI 'baseUrl/api/user/someId'."

I have mvc routes, web api routes and attribute routes all being registered on app start in this project but I would think that the default routes for this controller would be ~/userrest/... so I wouldn't think registering those would be the cause of this but I could be wrong.

Sample of what I want:

[RoutePrefix("api/user")]
public class UserRestController : ApiController
{
    UserService userService = new UserService();

    [Route("{id}"), HttpGet]
    public UserDTO Get(string id)
    {
        var user = userService.GetUserById(id);

        return user;
    }
}

I'd expect that ~/api/user/someId would work however I get the error mentioned above ("No HTTP resource was found that matches the request URI 'baseUrl/api/user/someId'.").

Full error xml:

<Error> 
    <Message> 
        No HTTP resource was found that matches the request URI     
        'baseUrl/api/user/someId';. 
    </Message> 
    <MessageDetail> 
        No type was found that matches the controller named 'user'. 
    </MessageDetail> 
</Error>
Teeknow
  • 1,015
  • 2
  • 17
  • 39
  • 1
    What's the problem? Sounds like it's working as expected. – Ant P Jan 10 '14 at 20:16
  • Actually, I think I understand what you're saying now. You need to share your routes. Just because no route has 'userrest' in its path explicitly doesn't mean it's not being picked up by a matching URL segment. – Ant P Jan 10 '14 at 20:18
  • I updated the question to show what I want to do. Hopefully that clears things up a little. Basically I want to be able to hit ~/api/user/someId but what I have in the second snippet of code is returning the error I had above. Here is the full xml returned: No HTTP resource was found that matches the request URI 'http://baseUrl/api/user/someId'. No type was found that matches the controller named 'user'. I have a feeling that the problem has to do with web api's routing but I'm not sure. – Teeknow Jan 10 '14 at 20:42
  • Can you share how your Global.asax looks like? – Kiran Jan 10 '14 at 21:41

1 Answers1

2

Based on your error description, you seem to have registered conventional routes before calling MapHttpAttributeRoutes. For example, you might be having a route like config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}"...). In this case the request like api/user/someid would match this route and the Web API would be looking for a controller of type UserController. Make sure to call MapHttpAttributeRoutes before this conventional route.

Basic rule: more specific routes should be registered before less-specific or generic routes.

Kiran
  • 56,921
  • 15
  • 176
  • 161