如何在 asp.net 核心中进行条件路由?

How to do conditional routing in asp.net core?

我想创建如下路由

[HttpPost("device/{id}/disable")]
[HttpPost("device/{id}/enable")]
public async Task SetDevice(stirng id, bool state)

并且当用户点击“device/{id}/disable”时,state var 将自动赋值为 false,反之亦然。我怎样才能做到这一点?谢谢

您有两个或更多选项:

使用路由名称:

[HttpPost("device/{id}/disable", Name = "disable_device")]
[HttpPost("device/{id}/enable", Name = "enable_device")]
public async Task SetDevice(stirng id){
   //...
   //get current route name here and implement your logic
   //...
{

如果您想知道如何在您的操作或剃须刀视图中获取路线名称,请参阅

或使用默认路由值:

[HttpPost("device/{id}/disable/{state=false}")]
[HttpPost("device/{id}/enable/{state=true}")]
public async Task SetDevice(stirng id, bool state){
   //...
{