0

In my controller I have action like this:

    [Route("api/[controller]")]
    [ApiController]
    public class ManageOPIdentifierController : ControllerBase
    {
        [HttpGet("[action]")]
        public OPIdentifiersVM Get(int pageSize, int pageNumber)
        {

How to add parameters pageSize and pageNumber to HttpGet? Because now when I have second method Get without parameters I get error because there are two routes with the same definition. How should looks the first HttpGet route?

[HttpGet("[action]/{pageSize}&{pageNumber}")]

Code above doesn't work

Edit: My question has been misunderstood. I have two methods Get:

[HttpGet("[action]")]
public OPIdentifiersVM Get(int pageSize, int pageNumber)

and

[HttpGet("[action]")]
public List<OPIdentifierVM> Get()

There is no problem to read values from parameters pageSize and pageNumber. The problem is that I have two methods with the same Http("[action]"). And I get error:

AmbiguousMatchException: The request matched multiple endpoints. Matches: 
ManageUuidWeb.Controllers.ManageOPIdentifierController.Get (OneProjectIdentifier.Web)
ManageUuidWeb.Controllers.ManageOPIdentifierController.Get (OneProjectIdentifier.Web)

If I good understood the comment I have to change name one of the method. But I want to know if it is possible to have two methods with the same name but with different parameters?

Robert
  • 2,451
  • 10
  • 55
  • 79
  • Check this out https://stackoverflow.com/questions/10658640/how-does-asp-net-web-api-handle-two-methods-with-names-starting-with-get. You should also change remove the & with / in the route – Harry Oct 24 '19 at 09:04

1 Answers1

2
[HttpGet("[action]")]
public OPIdentifiersVM Get([FromUri] int pageSize, [FromUri] int pageNumber)

(or [FromRoute] if you are using asp.net core) then it will be accessed by

http://localhost/api/ManageOPIdentifier/Get/10/1

or use query parameters for that (it would be better solution)

[HttpGet("[action]")]
public OPIdentifiersVM Get([FromQuery] int pageSize, [FromQuery] int pageNumber)

then

http://localhost/api/ManageOPIdentifier/Get?pageSize=10&pageNumber=1

also you can use simple [HttpGet] when your action name equals to http verb method name

Roman Marusyk
  • 21,493
  • 24
  • 66
  • 105