1

This is my code. I'm trying to overload GET with 2 function :

  • With one parameter
  • With two parameter

I'm getting Swagger error "Failed to load API definition". Why ?

[Route("api/[controller]")]
[ApiController] 
public class HospitalizedController : ControllerBase
{
    [HttpGet("")]
    public string Get(string MedicID)
    {
        string jsonData;

        string connString = gsUtils.GetDbConnectionString();
        // dosomething
    
    }

    [HttpGet("")]
    public string Get(string MedicID, string PatientID)
    {
        string jsonData;

        string connString = gsUtils.GetDbConnectionString();
        
        //do something
    }

}
4
  • 1
    Because the endpoint of the two methods is the same (/api/hospitalized). Maybe you can use: /api/hospitalized/{medicId} and /api/hospitalized/{medicId}/patients/{patientId} ? Commented Nov 6, 2022 at 10:34
  • Thank you: how to declare different endpoint ? Commented Nov 6, 2022 at 10:35
  • 1
    I've written the comment as an answer with an example for the Route attribute Commented Nov 6, 2022 at 10:38
  • 1
    have you tried ActionName attribute Commented Nov 6, 2022 at 10:38

1 Answer 1

2

The error "Failed to load API definition" occurs because the two methods are on the same Route.

You can specify a more specific route to distinguish them, like this:

[Route("api/[controller]")]
[ApiController] 
public class HospitalizedController : ControllerBase
{
    [HttpGet]
    [Route("{medicId}")]
    public string Get(string medicID)
    {
    
    }

    [HttpGet]
    [Route("{medicId}/patients/{patientId}")]
    public string Get(string medicID, string patientID)
    {

    }

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.