WebApi RouteAttribute 错误

Error with WebApi RouteAttribute

这是我的前缀属性

 [RoutePrefix("news/farms")]
 public class farmsController : ApiController

我想删除基于 farmid 的行,但我想要这样的 url 结构

/news/farms/{farmId}?version={Version}

我试过路线 url 就像下面的代码

 [HttpDelete, Route("{farmId}?version={Version}", Name = "Deletefarm")]

但是显示

The route template cannot start with a '/' or '~' character and it cannot contain a '?' character.

请任何人告诉我有没有其他方法可以解决此错误?

为什么要尝试在路由中添加版本作为查询参数?这不是您想要的控制器路由吗?

尽管如此: 这行得通吗?

[HttpDelete]
public async Task<IActionResult> Delete(int id, [FromQuery] string version)
{
 //...your code
}

使用 [FromQuery] 你指定你想要它来自....查询;)

URI 路径中不能包含查询字符串,因此不允许,请参考 - https://www.rfc-editor.org/rfc/rfc3986#section-3.3

你想要的答案是-

[HttpDelete]
public IActionResult DeleteFarm(int farmId, [FromQuery] string version)
{
    //
}

像您的示例一样设置 RoutePrefix

[RoutePrefix("news/farms")]
public class FarmsController : ApiController

还有你的删除方法:

[HttpDelete]
[Route("{farmId}")]
public void Delete(int farmId, string version)

那么这应该可行:

.../news/farms/1?version=myVersion