I have two API endpoints defined as such:
[Route("create")]
[HttpPost]
[ResponseType(typeof(User))]
public async Task<IHttpActionResult> CreateUser(User user)
[Route("login")]
[HttpPost]
[ResponseType(typeof (User))]
public async Task<IHttpActionResult> Login(string email, string password)
And the controller defined as
[RoutePrefix("api/users")]
public class UserController : ApiController
When I call it with this information (both in plain chrome, my app and the Postman application)
POST /api/users/login HTTP/1.1
Host: mysite.azurewebsites.net
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
email=somemail&password=somepw
I receive a 404:
{
"Message": "No HTTP resource was found that matches the request URI 'http://mysite.azurewebsites.net/api/users/login'.",
"MessageDetail": "No action was found on the controller 'User' that matches the request."
}
It does work for another route which I can call with /api/users/1:
[Route("{id:int}")]
[HttpGet]
[ResponseType(typeof(User))]
public async Task<IHttpActionResult> GetUser(int? id)
Can't I explicitly define such endpoints? I tried creating a custom route but this made no difference (I placed this before the default route and after calling config.MapHttpAttributeRoutes()).
config.Routes.MapHttpRoute(
name: "Login",
routeTemplate: "api/users/login",
defaults: new { controller = "User", action = "Login" }
);
Note that explicitly defining the route as [Route("~api/users/login")] didn't work either.
I have also noticed that routes in my other controller don't seem to work anymore. More specifically I have these definitions:
[RoutePrefix("api/movies")]
public class MovieController : BaseController
[Route("~api/genres")]
[HttpGet]
[ResponseType(typeof(IEnumerable<Genre>))]
public IHttpActionResult GetGenres()
[Route("~api/genres/{id:int}")]
[HttpGet]
[ResponseType(typeof(IEnumerable<MovieResult>))]
public IHttpActionResult GetMoviesForGenre(int id)
[Route("{id:int}")]
[HttpGet]
[ResponseType(typeof(Movie))]
public IHttpActionResult GetMovieDetails(int id)
Of these options, only a call to /api/movies/16 succeeds, the others return
No type was found that matches the controller named 'genres'.
Is there something elementary I'm overlooking?
I have made the genre routes available again by changing them to genres and genres/{id:int} and adding this route
config.Routes.MapHttpRoute(
name: "test",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
but I would assume that this shouldn't be necessary. For some reason the request to /api/movies/genres works while /api/users/login doesn't. I did notice that creating a GET method with URI /api/users/genres DOES work so I believe it must have something to do with that. Why won't it find my POST-methods?