0

I am trying to make an HTTP Get REST API that will take 3 parameters. However, I am getting errors or the 3 parameters are not getting passed.

I get a build error - "HttpGetAttribute does not contain a constructor that takes 3 arguments"

Here is the way, I am checking it.

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

I have removed the line HttpGet but it doesn't help.

[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();
}

I expect the URL to pass these parameters to the REST GET API.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Prawn Hongs
  • 441
  • 1
  • 5
  • 17

1 Answers1

1

You need to make 2 changes to the routing:

[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();
}

If you want to make the parameters required then you can use the [Required] attribute.

haldo
  • 14,512
  • 5
  • 46
  • 52
  • "See attribute routing: Attribute Routing in ASP.NET Web API 2" but the question is about ASP.NET Core, not ASP.NET Web API 2, that link is useless. Besides that, why would you put a Route **and** a HttpGet attribute? Route is for Controllers to set up the base route, not for Actions – Camilo Terevinto Jan 23 '19 at 00:56
  • @CamiloTerevinto you just removed the .asp.net-webapi tag? The question and title did reference Asp.Net core WebAPI and WebAPI controller until your edit. OP also states they're making a REST API. Seems to fit to my interpretation. Please explain? – haldo Jan 23 '19 at 00:59
  • No, that's wrong, I removed the ASP.NET Core WebAPI tag, because ASP.NET Core Web API is not being used in the question – Camilo Terevinto Jan 23 '19 at 01:00
  • it works, how do I put that a, b, c are required parameters? – Prawn Hongs Jan 23 '19 at 01:32
  • @PrawnHongs you can try adding the `[Required]` attribute to each parameter (`[Required] int a` ). Another option is to create a model with `[Required]` decorating the properties. – haldo Jan 23 '19 at 08:48