Sunil Chaudhry
Sunil Chaudhry

Reputation: 263

Unable to hit API Endpoint containing generic type

I am working on a Web API, its working fine except one endpoint and returning 404 for the same. Below is my code here I am using generic type to add 2 input parameters.

Method:

    [AllowAnonymous]
    [HttpGet("{number1}/{number2}")]
    [IgnoreAntiforgeryToken]
    public dynamic AddTwoInputs<T>(T number1, T number2)
    {
        dynamic a = number1;
        dynamic b = number2;
        return a + b;
    }

I am trying to hit it from postman Get method with URL "domain/api/Users/AddTwoInputs/1/2". Please suggest the solution.

Upvotes: 1

Views: 545

Answers (1)

NotFound
NotFound

Reputation: 6147

Quite sure that aspnet just ignores the method as generic types are not going to work for controller methods.

Your code is a good example why. The url is just a string and there's no way of knowing for your API what parameter type you intend it to be. If it's intended to be numbers you'll get the result 3. If it's a string it'll result in 12.

Upvotes: 1

Related Questions