【发布时间】:2014-09-11 01:37:44
【问题描述】:
我有两个这样定义的 API 端点:
[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)
而控制器定义为
[RoutePrefix("api/users")]
public class UserController : ApiController
当我使用此信息调用它时(在纯 chrome、我的应用程序和 Postman 应用程序中)
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
我收到 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."
}
它确实适用于我可以使用/api/users/1 调用的另一条路线:
[Route("{id:int}")]
[HttpGet]
[ResponseType(typeof(User))]
public async Task<IHttpActionResult> GetUser(int? id)
我不能明确定义这样的端点吗?我尝试创建一个自定义路由,但这没有任何区别(我将它放在默认路由之前和调用config.MapHttpAttributeRoutes() 之后)。
config.Routes.MapHttpRoute(
name: "Login",
routeTemplate: "api/users/login",
defaults: new { controller = "User", action = "Login" }
);
请注意,将路由明确定义为 [Route("~api/users/login")] 也不起作用。
我还注意到我的另一个控制器中的路由似乎不再起作用了。更具体地说,我有这些定义:
[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)
在这些选项中,只有对/api/movies/16 的调用成功,其他选项返回
找不到与名为“流派”的控制器匹配的类型。
我忽略了一些基本的东西吗?
我已将流派路线更改为genres 和genres/{id:int} 并添加此路线,从而使它们再次可用
config.Routes.MapHttpRoute(
name: "test",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
但我认为这不是必需的。由于某种原因,对/api/movies/genres 的请求有效,而/api/users/login 无效。我确实注意到使用 URI /api/users/genres DOES 创建一个 GET 方法是可行的,所以我相信它一定与此有关。为什么它找不到我的 POST 方法?
【问题讨论】:
标签: c# asp.net-web-api routing asp.net-web-api2