I have the following Controller in my ASP.NET Core project:
[Route("api/[controller]")]
[ApiController]
public class MainController : ControllerBase
{
[HttpGet("DataQ")
public List<T> GetData<T>(int id) where T : class => new List<T>() { default(T) };
[HttpGet("T1")
public string Testing() => "Testing";
}
As you see, the controller isn't generic but it contains a generic method.
With the HttpGet attribute I can set what URI will be used on call.
The Testing method can be called (so the Controller is visible). However, it seems that the generic method does not exist in the controller.
According to the docs, only the generic controllers require extra measures to be discoverable:
By default, ASP.NET Core MVC ignores generic controllers (for example,
SomeController<T>). This sample uses a controller feature provider that runs after the default provider and adds generic controller instances for a specified list of types (defined in EntityTypes.Types)
Can I somehow call the generic method or I should implement inheriting controllers that call the method?
T?Tshould be inferred from the incoming parameter.Tfrom anint) so it won't work on the web. Even if you put aTin the parameters, that's not how it works. Inferring something means it's known at compile time. A compiler cannot infer something that does not happen until after the compilation is done.Tas parameter and I send a5.0, is that a float or a double? Or a decimal? Or maybe an int, because JSON does not even know the difference, it's all number?