简体   繁体   English

请求的资源不支持HTTP方法GET

[英]The requested resource does not support HTTP method GET

When I run this url: /api/users/1 it does only map to the Delete action when I use the HttpDelete-Attribute. 当我运行这个url: /api/users/1它只会在我使用HttpDelete-Attribute时映射到Delete操作。 What is the reason for this behavior? 这种行为的原因是什么?

Else I get his message: The requested resource does not support HTTP method GET 否则我收到他的消息:请求的资源不支持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");
    }
}

These are my routes: 这些是我的路线:

 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 }
            );

By convention, HTTP verbs will match to an action name that is prefixed with that HTTP verb. 按照惯例,HTTP谓词将匹配以该HTTP谓词为前缀的操作名称。

So, it's complaining you don't have an action for an HTTP GET, which is the verb used when you make a simple request with your browser. 所以,它抱怨你没有HTTP GET的动作,这是你用浏览器发出简单请求时使用的动词。 You need an action named something like: 您需要一个名为的行为:

public HttpResponseMessage Get(int id)

or even 甚至

public HttpResponseMessage GetUser(int id)

Obviously if you make a request with DELETE, it will map to the Delete action you have defined. 显然,如果您使用DELETE发出请求,它将映射到您已定义的删除操作。

Ref: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api 参考: http//www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

"To find the action, Web API looks at the HTTP method, and then looks for an action whose name begins with that HTTP method name. For example, with a GET request, Web API looks for an action that starts with "Get...", such as "GetContact" or "GetAllContacts". This convention applies only to GET, POST, PUT, and DELETE methods. You can enable other HTTP methods by using attributes on your controller. We'll see an example of that later." “要查找操作,Web API会查看HTTP方法,然后查找名称以该HTTP方法名称开头的操作。例如,对于GET请求,Web API会查找以”Get ..“开头的操作。 “,例如”GetContact“或”GetAllContacts“。此约定仅适用于GET,POST,PUT和DELETE方法。您可以通过控制器上的属性启用其他HTTP方法。我们稍后会看到一个示例“。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM