当我运行这个 url 时:/api/users/1
它只在我使用 HttpDelete-Attribute 时映射到 Delete 操作。这种行为的原因是什么?
否则我会收到他的消息:请求的资源不支持 HTTP 方法 GET
[RoutePrefix("api/users")]
public class UserController : ApiController
{
private readonly IUserService _userService;
public UserController(IUserService userService)
{
_userService = userService;
}
[Route("")]
public HttpResponseMessage Get()
{
return Request.CreateResponse<IEnumerable<UserDTO>>(HttpStatusCode.OK, _userService.GetUsers());
}
[Route("{id:int}")]
[HttpDelete]
public HttpResponseMessage Delete(int id)
{
_userService.Delete(id);
return Request.CreateResponse(HttpStatusCode.OK, "User was deleted successfully");
}
}
这些是我的路线:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Home", action = "Start", id = RouteParameter.Optional }
);