0

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?

4
  • How would you want to call such a method from the web? How would you expect to be able to pass T? Commented Aug 16, 2018 at 9:10
  • @nvoigt I'd expect the actual parameter to be serialized to JSON and in the server side the type of T should be inferred from the incoming parameter. Commented Aug 16, 2018 at 9:13
  • That does not work in C# code (you cannot infer a T from an int) so it won't work on the web. Even if you put a T in 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. Commented Aug 16, 2018 at 9:15
  • 1
    Imagine you pick T as parameter and I send a 5.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? Commented Aug 16, 2018 at 9:17

1 Answer 1

3

ASP.NET does not match parameters the way you expect. It cannot find out which of your classes in your project may or may not fit the JSON sent. You need to give it a specific class it can then match the JSON input against and either succeed or fail.

ASP.NET needs to have a specific controller. That can be a generic controller, but the types need to be known. You cannot have a generic and say "I dunno, the types... just use what comes in". As they need to be known, having a generic method going to the outside is not possible.

You can implement generic controllers as per your link, or you can inherit controllers that call the generic method in their C# code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.