Alma
Alma

Reputation: 4390

Is is possible to have 2 methods with same name in WebAPI and same Parameter Type?

Is it possible to have 2 methods name with same name in WebApi and same parameter type but different parameter?

For example one is get Product by Yearid

And the other one is get Product by Productid

And I like to have this Rout:

Products?yearId=10
Products/15

I know I can have different name but my boss likes to have same name I wonder if it is possible.

and these are methods:

    [HttpGet]
    [Route("Products/{yearId}")]
    public async Task<IEnumerable<Make>> GetProductsYearId(int yearId)
    {
        ....
    }

    [HttpGet]
    [Route("Products/{makeid}")]
    public async Task<Make> GetProductById(int makeid) 
    {
         .....      
    }

Not sure how the [Route] should looks like to get this end result.

Upvotes: 1

Views: 1115

Answers (1)

RJ Cuthbertson
RJ Cuthbertson

Reputation: 1518

As Brendan Green commented, you would need to have two separate routes defined. Otherwise there wouldn't be a way to determine which method you actually intended to invoke:

[HttpGet]
[Route("Products/Year/{yearId}")]
public async Task<IEnumerable<Make>> GetProductsYearId(int yearId)
{
    ...
}

[HttpGet]
[Route("Products/Make/{makeid}")]
public async Task<Make> GetProductById(int makeid) 
{
     ... 
}

Upvotes: 2

Related Questions