如何将3个参数传递给动作?

时间:2019-01-23 00:22:44

标签: c# asp.net-core

我正在尝试制作一个将使用3个参数的HTTP Get REST API。 但是,我遇到了错误,或者3个参数都没有传递。

我收到一个构建错误-"HttpGetAttribute does not contain a constructor that takes 3 arguments"

这是方法,我正在检查。

https://localhost:44312/api/test/1/2/3

我删除了HttpGet行,但无济于事。

[Route("api/controller/{a}/{b}/{c}")]
[HttpGet("{a}", "{b}", "{c}")]
public string Get(int a, int b, int c){
  int sum = a + b + c;
  return sum.ToString();
}

我希望URL将这些参数传递给REST GET API。

2 个答案:

答案 0 :(得分:1)

您需要对路由进行2项更改:

[Route("api/[controller]")]     // [controller]
[HttpGet("{a}/{b}/{c}")]        
public string Get(int a, int b, int c)
{
    int sum = a + b + c;
    return sum.ToString();
}

如果要设置必需的参数,则可以使用[Required] attribute

答案 1 :(得分:-1)

您不能将多个参数传递给HttpGet。您需要像这样指定它们。

[HttpGet("[controller]/[action]/{id}")]

请参阅此链接,以更好地了解如何在.Net Core中使用Routing

Routing to Controller Actions in ASP.Net Core