问题描述
我正在尝试使用Ktor routing,我想弄清楚以下代码的工作原理:
application.install(Routing) {
get("/") {
call.respondText("Hello,World!")
}
get("/bye") {
call.respondText("Good bye,World!")
}
}
变量call
属性从何而来?我查看了https://api.ktor.io/1.4.0/io.ktor.routing/-route/index.html,但找不到答案。
我知道get
的第二个参数:
get("/bye") {
call.respondText("Good bye,World!")
}
期望一个lambda。但是call
变量必须纳入范围。这不是明确的。
解决方法
call
是PipelineContext
的扩展变量(获取器),正如您所提到的,它是get
方法的第二个参数接收者。
inline val PipelineContext<*,ApplicationCall>.call: ApplicationCall get() = context
详细了解extensions