'How to form a URL to access the webapi controller?

I would like to know how we will create a Route URL to access the above function. Error message comes stating that I cannot access the controller

 [HttpGet]
        [Route("api/TeacherData/ListTeachers/{SearchKey?}&{order?}")]
        public List<Teacher> ListTeachers(string SearchKey = null, string order = null)
        {
         
        }


Solution 1:[1]

I know it's your API and your design, but following the REST API patterns we should stick to simple API URLs, something like api/teachers could be easier to understand by the consumers if they know that the endpoint uses the GET method. About your actual question, you could change the code to use [FromQuery] to expect parameters that should come from the query string:

[HttpGet]
[Route("api/teachers")]
public List<Teacher> ListTeachers([FromQuery] string searchKey = null, [FromQuery] string order = null)
{
}

Then, from the consumer side, you could trigger this endpoint using the following URL:

GET http://myapi.com/api/teachers?searchKey=keyValue&order=orderValue

If you keep your URL structure it should something like this:

GET http://myapi.com/api/TeacherData/ListTeachers?searchKey=keyValue&order=orderValue

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JuanG