Kotlin:Ktor 如何将文本响应为 html
Kotlin: Ktor how to respond text as html
我想使用库 kotlin-html
生成 html 而不是 kotlinx.html。
这个库只产生一个 html-text:
p("A paragraph").render()
// => <p>A paragraph</p>
但我找不到如何回复 html 而不是使用 Ktor
的文本
fun Routing.root() {
get("/") {
call.respondText {"<p>A paragraph</p>"}
}
}
此代码将生成包含文本 <p>A paragraph</p>
而非 html 页面的页面。而且 call.respondHtml
似乎只适用于 kotlinx.html DSL。我怎样才能使用纯文本来做到这一点?
您可以将 ContentType
参数指定为 ContentType.Text.Html
至 respondText
call.respondText("<p>foo</p>", ContentType.Text.Html)
如果没有ContentType
默认使用ContentType.Text.Plain
Ktor 有一个特殊的模块用于 kotlinx.html
,所以你可以使用
call.respondHtml {
head {
title { +"Async World" }
}
body {
h1(id = "title") {
+"Title"
}
}
}
在此处查看详细信息:https://ktor.io/servers/features/templates/html-dsl.html
我想使用库 kotlin-html
生成 html 而不是 kotlinx.html。
这个库只产生一个 html-text:
p("A paragraph").render()
// => <p>A paragraph</p>
但我找不到如何回复 html 而不是使用 Ktor
的文本fun Routing.root() {
get("/") {
call.respondText {"<p>A paragraph</p>"}
}
}
此代码将生成包含文本 <p>A paragraph</p>
而非 html 页面的页面。而且 call.respondHtml
似乎只适用于 kotlinx.html DSL。我怎样才能使用纯文本来做到这一点?
您可以将 ContentType
参数指定为 ContentType.Text.Html
至 respondText
call.respondText("<p>foo</p>", ContentType.Text.Html)
如果没有ContentType
默认使用ContentType.Text.Plain
Ktor 有一个特殊的模块用于 kotlinx.html
,所以你可以使用
call.respondHtml {
head {
title { +"Async World" }
}
body {
h1(id = "title") {
+"Title"
}
}
}
在此处查看详细信息:https://ktor.io/servers/features/templates/html-dsl.html