首页 > 解决方案 > Put request to /api/controller/:id instead of /api/controller?id=:id

问题描述

I'm currently doing this:

[HttpPut]
public void Edit(int id, Model model)
{
    ...
}

Which gives me the endpoint /api/controller?id=66 instead of what I want: /api/controller/66

标签: asp.net-coreasp.net-core-webapi

解决方案


To get what you want -- api/controller/66 on your PUT request, your HTTP verb attribute should be modified to [HttpPut("{id}")]

And the further reason why your id is obtained from the query string by default is that the parameter binding in the case of PUT request works in such a way that the primitive type is bound from request query string and the complex type from request body.

A brief of the parameter binding rules is listed out in this answer.


推荐阅读