2

I'd like to create routings like below

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

And when the user hit the "device/{id}/disable", the state var will automatically assigned with false, and vise versa. How can I achieve that? Thanks

0

1 Answer 1

1

You have two options or maybe more:

Use route names:

[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
   //...
{

If you want to know how to get the route name in your action or razor view see get current route name.

Or use default route values:

[HttpPost("device/{id}/disable/{state=false}")]
[HttpPost("device/{id}/enable/{state=true}")]
public async Task SetDevice(stirng id, bool state){
   //...
{
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, @Mohammad Barbast, I tested your solution but [HttpPost("device/{id}/disable/{state?=false}")] causing RoutePatternException: The route parameter name 'state?' is invalid. Route parameter names must be non-empty and cannot contain these characters: '{', '}', '/'. The '?' character marks a parameter as optional, and can occur only at the end of the parameter. The '*' character marks a parameter as catch-all, and can occur only at the start of the parameter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.