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,571
  • 10
  • 63
  • 95
  • 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

2 Answers2

3
[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
  • 23,328
  • 24
  • 73
  • 116
0

Answer: Yes, you can use two methods with the same name in C# as long as they have different parameters signatures.

But...

...in the world of Attribute Routing, they cannot share the same name if you use [action] in your routing template, which maps the URL path directly to the method name as its endpoint. Both your Controller Methods share the same name so both map to the same routes or URL and conflict.

Your Attribute [action] route attributes both map to Get, the action method name. So both would map to a URL, like: https://example.com/get and conflict.

[HttpGet("[action]")]
public OPIdentifiersVM Get (int pageSize, int pageNumber)
[HttpGet("[action]")]
public List<OPIdentifierVM> Get()

// To the Routing Middleware, they are interpreted as...

[HttpGet("[action]")]
public Get ()
[HttpGet("[action]")]
public Get ()

// This becomes two conflicting URLS as:
// https://example.com/get
// https://example.com/get

To fix it just change your method name to something new. Or, you can keep the name but change the Attribute route value to something like:

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

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


// This becomes two new URLS as:
// https://example.com/route1
// https://example.com/route2
Stokely
  • 12,444
  • 2
  • 35
  • 23