Kotlin:Ktor如何将文本响应为html
我想使用libraryKotlin:Ktor如何将文本响应为html,kotlin,ktor,Kotlin,Ktor,我想使用librarykotlinhtml来生成html,而不是kotlinx.html。 此库仅生成一个html文本: p("A paragraph").render() // => <p>A paragraph</p> p(“段落”).render() //=>一段 但是我找不到如何使用Ktor来响应html而不是文本 fun Routing.root() { get("/") { call.respondText {"<p&g
kotlinhtml
来生成html,而不是kotlinx.html。
此库仅生成一个html文本:
p("A paragraph").render()
// => <p>A paragraph</p>
p(“段落”).render()
//=>一段
但是我找不到如何使用Ktor来响应html而不是文本
fun Routing.root() {
get("/") {
call.respondText {"<p>A paragraph</p>"}
}
}
fun Routing.root(){
获取(“/”){
call.respondText{“段落”}
}
}
此代码将生成一个包含文本
段落的页面,而不是html页面。而且似乎call.respondHtml
只适用于kotlinx.html DSL。如何使用纯文本执行此操作?您可以为ContentType
参数指定respondText
call.respondText("<p>foo</p>", ContentType.Text.Html)
call.respondText(“foo”,ContentType.Text.Html)
如果未提供ContentType
,则默认情况下使用ContentType.Text.Plain
。您可以为ContentType
参数指定respondText
call.respondText("<p>foo</p>", ContentType.Text.Html)
call.respondText(“foo”,ContentType.Text.Html)
如果未提供ContentType
,则默认情况下使用ContentType.Text.Plain
。Ktor有一个特殊模块用于处理kotlinx.html
,因此您可以使用
call.respondHtml {
head {
title { +"Async World" }
}
body {
h1(id = "title") {
+"Title"
}
}
}
请参阅此处的详细信息:Ktor有一个用于处理kotlinx.html
的特殊模块,因此您可以使用
call.respondHtml {
head {
title { +"Async World" }
}
body {
h1(id = "title") {
+"Title"
}
}
}
详见此处:啊,对不起,我误解了你的问题。但是,让一个答案在这里为正在搜索的人…它对我不起作用。有个错误。“找不到名称为id的参数。id是什么?我必须首先实现的变量?啊,抱歉,我误解了你的问题。但让我在这里为搜索的人提供答案……这对我不起作用。出现错误。”找不到名称为id的参数。id是什么?一个我必须首先实现的变量?