2

I am trying to create a generic web api in .net core 3.0. We have also implemented swagger in the same api project.

However, I am facing issue, as I am not able to see web api in swagger when I am trying to test/execute it locally. So, how to make swagger recognize this web api? Any specific setting we need to do it ?

Here is the api code.

[Route("api/[controller]/[action]")]
[ApiController]
public class GenericLookUpController : ControllerBase
{
    [HttpPost]
    public IActionResult GenericPostMethod<T>([FromBody] T record) where T : class
    {
        // some logic
    }
}

When I write non generic web api, it's visible in swagger. I have also made the controller class as generic, but it doesn't resolved the issue.

Basically, I don't want entire controller as generic controller but just few web api's for lookup tables. These generic web api's will be used for CRUD operation for lookup tables.

So, what changes I need to make so web api will be visible in swagger?

Any help on this appreciated !

Majid
  • 3,128
  • 1
  • 26
  • 31
XamDev
  • 3,377
  • 12
  • 58
  • 97
  • how are you generating it in swagger? id also avoid generic apis... – Daniel A. White Feb 14 '20 at 14:58
  • You may need to plug in as described [here](https://stackoverflow.com/questions/53762697/swagger-add-document-and-operation-for-a-generic-controller) – mu88 Feb 14 '20 at 14:59
  • @DanielA.White I have swagger configuration in startup.cs. What is the reason to avoid generic apis? Basically I have 30 odd lookup tables, for which I want to implement CRUD operation. So instead of writing api for each table, i am trying to write in generic way – XamDev Feb 14 '20 at 15:35
  • @mu88 Can you explain how does it works ? – XamDev Feb 14 '20 at 15:36
  • @XamDev I don't have a Proof of Concept - but I could imagine the following way: 1. hook into Swashbuckle (see first link) 2. Determine the necessary information on your own (e. g. reflection) 3. Process the retrieved information and add it to Swashbuckle. – mu88 Feb 17 '20 at 06:58

0 Answers0