简体   繁体   English

如何形成一个URL访问webapi controller?

[英]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我想知道我们将如何创建一个路由 URL 来访问上面的 function。错误消息说我无法访问 controller

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

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.我知道这是你的 API 和你的设计,但是按照 REST API 模式,我们应该坚持使用简单的 API URL,如果消费者知道端点使用 GET 方法,那么像api/teachers这样的东西可能更容易被消费者理解。 About your actual question, you could change the code to use [FromQuery] to expect parameters that should come from the query string:关于您的实际问题,您可以更改代码以使用[FromQuery]来期望应该来自查询字符串的参数:

[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:然后,从消费者端,您可以使用以下 URL 触发此端点:

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

If you keep your URL structure it should something like this:如果你保留你的 URL 结构,它应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM