属性路由不工作在Put请求上。

时间:2022-03-18 11:04:47

In my simple WebApi project I'm only using attribute routing by doing this:

在我简单的WebApi项目中,我只使用属性路由:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
    }
}

I have this controller:

我有这个控制器:

[RoutePrefix("myprefix")]
public class SomeController : ApiController
{
    [HttpGet]
    [Route("v2/test/dosomething")]
    public IHttpActionResult TestIt()
    {
        return Ok("Test ok");
    }

    [HttpPut]
    [Route("v2/myaction/somethingelse")]
    public async Task<IHttpActionResult> MyAction(string message)
    {
        return Ok("Put ok");
    }
}

I'm accessing the above as follows:

我访问上述内容如下:

GET http://localhost/myapp/myprefix/v2/test/dosomething
PUT http://localhost/myapp/myprefix/v2/myaction/somethingelse

The GET works fine. However, the PUT returns:

得到好工作。然而,返回:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost/myapp/myprefix/v2/myaction/somethingelse'.",
    "MessageDetail": "No action was found on the controller 'Some' that matches the request."
}

What am I missing?

我缺少什么?

2 个解决方案

#1


0  

The default way of sending data with PUT requests are through the body. So first step is to add a [FromBody] infront of the parameter. If you sending data with content type of json then I would create a object as parameter instead which contains a message parameter.

用PUT请求发送数据的默认方式是通过body。所以第一步是在参数的前面添加一个[FromBody]。如果您以json的内容类型发送数据,那么我将创建一个对象作为参数,而不是包含一个消息参数。

public class jsonMessage
{
    public string message {get; set; }
}

Use the jsonMessage class as the parameter with the [FromBody] in the controller method as this

在控制器方法中使用jsonMessage类作为[FromBody]的参数。

[HttpPut]
[Route("v2/myaction/somethingelse")]
public async Task<IHttpActionResult> MyAction([FromBody]jsonMessage message)
{
    return Ok("Put ok");
}

Then when making request to the endpoint add a request body as this {"message":"waaaasssaaa"} and send it with Content-Type: 'application/json'

然后,当向端点发出请求时,将请求主体添加为这个{“message”:“waaaasssaaa”,并将其发送到Content-Type:“应用程序/json”

#2


0  

 [HttpPut]
 [Route("v2/myaction/somethingelse/{message}")]
 public async Task<IHttpActionResult> MyAction(string message)
 {
     return Ok("Put ok");
 }

Actually you have to specifie your parameter also, in your route.

实际上,在你的路线中,你也必须指定你的参数。

#1


0  

The default way of sending data with PUT requests are through the body. So first step is to add a [FromBody] infront of the parameter. If you sending data with content type of json then I would create a object as parameter instead which contains a message parameter.

用PUT请求发送数据的默认方式是通过body。所以第一步是在参数的前面添加一个[FromBody]。如果您以json的内容类型发送数据,那么我将创建一个对象作为参数,而不是包含一个消息参数。

public class jsonMessage
{
    public string message {get; set; }
}

Use the jsonMessage class as the parameter with the [FromBody] in the controller method as this

在控制器方法中使用jsonMessage类作为[FromBody]的参数。

[HttpPut]
[Route("v2/myaction/somethingelse")]
public async Task<IHttpActionResult> MyAction([FromBody]jsonMessage message)
{
    return Ok("Put ok");
}

Then when making request to the endpoint add a request body as this {"message":"waaaasssaaa"} and send it with Content-Type: 'application/json'

然后,当向端点发出请求时,将请求主体添加为这个{“message”:“waaaasssaaa”,并将其发送到Content-Type:“应用程序/json”

#2


0  

 [HttpPut]
 [Route("v2/myaction/somethingelse/{message}")]
 public async Task<IHttpActionResult> MyAction(string message)
 {
     return Ok("Put ok");
 }

Actually you have to specifie your parameter also, in your route.

实际上,在你的路线中,你也必须指定你的参数。