almighty All!
Second day passed... Need Help!
I have two controllers: base DocumentController and derived InvoiceController in ASP.NET C# WebAPI application**:**
[ApiController]
[Route("Documents")]
public class DocumentController : ControllerBase
{
[HttpGet]
public virtual IEnumerable<Document> Get()
{
... //some implementation
}
}
[ApiController]
[Route("Documents/Invoices")]
public class InvoiceController : DocumentController
{
[HttpGet]
public new IEnumerable<Invoice> Get()
{
... //some implementation
}
}
I need first of them to show all documents, and the second one to show invoices only
Classes Document and Invoice are also base and inherited one
public class Document { ... }
public class Invoice: Document { ... }
when I use override keyword in derived controller, like this:
public override IEnumerable<Document> Get()
everything works fine, except that I need a list on Invoices, not Documents
But when I use new keyword, like this:
public new IEnumerable<Invoice> Get()
I get an error:
AmbiguousMatchException: The request matched multiple endpoints. Matches: InvoiceController.Get DocumentsController.Get
It means, that I have two methods = one from parent "DocumentController", and another one from child "InvoiceController"
It can be solved by adding some additional route attribute to child controller - for example "aaa" - in this case I have two GET endpoints in child controller: Documents/Invoices - with a list of Documents (inherited from the base class) and Documents/Invoices/aaa - with a list of Invoices (from the new implementation in derived class)
but that is ugly
tried use different route attributes and so on...
[ApiController] [Route("[controller]")] public class DocumentsController : ControllerBase { [HttpGet] public async <IEnumerable<Document>> Get(string? FromDate, string? ToDate) { ... } [HttpGet] [Route("invoices")] public async <IEnumerable<Invoice>> GetInvoices(string? FromDate, string? ToDate) { ... } }Thanks, man! So easy, so good!