I am using a custom HTTP controller selector for my API version.
config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceSelector(config));
Below is my controller with actions:
[RoutePrefix("api/v1/messages")]
public class MessagesController : ApiController
{
[Route("unreadall")]
public IEnumerable<long> UnreadAll()
{
}
[Route("{type}/unreadall")]
public IEnumerable<long> UnreadAll(string type)
{
}
[Route("unreadnext")]
public long UnreadNext()
{
}
[Route("{type/}unreadnext")]
public long UnreadNext(string type)
{
}
[Route("{id:long}/markasread")]
[HttpPut]
public string MarkAsRead(long id)
{
}
[Route("{id:long}")]
public string Get(long id)
{
}
[Route("")]
[HttpPost]
public long Post(string message)
{
}
}
Below is my route configuration:
config.Routes.MapHttpRoute(
name: "DefaultApi1",
routeTemplate: "api/{version}/{controller}/{id}/{action}"
);
config.Routes.MapHttpRoute(
name: "DefaultApi2",
routeTemplate: "api/{version}/{controller}/{action}"
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{version}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
When I test my routes, do the following work.
/api/v1/messages/unreadall
/api/v1/messages/unreadnext
/api/v1/messages/123/markasread
But the routes listed below also indicate the same actions.
/api/v1/messages/type/unreadall
/api/v1/messages/type/unreadnext
And I get errors for the rest of my routes.
/api/v1/messages/123
Error:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:59411/api/v1/messages/123'.",
"MessageDetail": "No action was found on the controller 'MessagesController' that matches the name '123'."
}
POST: /api/v1/messages
Error:
{
"Message": "The requested resource does not support http method 'POST'."
}
Can someone tell me what I am doing wrong with my route setup? or can someone send the working route configuration for my scripts above?
Appreciate your help!
Greetings