Home > Enterprise >  Why swagger return "Failed to load api definition"?
Why swagger return "Failed to load api definition"?

Time:11-07

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
    }

}

CodePudding user response:

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

    }

}
  • Related