Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How should I resolve ASP.NET Core multiple endpoints error

My controller is:

[Route("api/[controller]")]
[ApiController]
public class SubjectController : ControllerBase
{
    private TourActivityExpenseContext tourActivityExpenseContext = new TourActivityExpenseContext();
      
    [HttpGet]
    public IEnumerable<SubjectTo> Get()
    {
        var subjects = tourActivityExpenseContext.SubjectTos.ToList();
        return subjects;
    }

    [HttpGet("{subject}")]
    public int GetSubjectId(String subject)
    {
        var subjects = tourActivityExpenseContext.SubjectTos.ToList();
        var isSub = subjects.FirstOrDefault(x => x.Subject == subject);
        int id = isSub.Id;
        return id;
    }
    
    [HttpGet("{id}")]
    public string Get(int id)
    {
        var subjects = tourActivityExpenseContext.SubjectTos.ToList();
        var isSub = subjects.FirstOrDefault(x => x.Id == id);
        String sub = isSub.Subject;
        return sub;
    }
}

When requested in Postman with an URL like

http://localhost:5000/api/Subject/Meeting

I get an error like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:

TourExp.Controllers.SubjectController.Get (TourExp)
TourExp.Controllers.SubjectController.GetSubjectId (TourExp)

>Solution :

Try to give them a specific route :

        [HttpGet("GetSubject/{subject}")]
        public int GetSubjectId(String subject)
        {

            var subjects = tourActivityExpenseContext.SubjectTos.ToList();
            var isSub = subjects.FirstOrDefault(x => x.Subject == subject);
            int id = isSub.Id;
            return id;
        }

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading