stighy
stighy

Reputation: 7170

Why swagger return "Failed to load api definition"?

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

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
    }

}

Upvotes: 1

Views: 5787

Answers (1)

Randy
Randy

Reputation: 563

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)
    {

    }

}

Upvotes: 2

Related Questions