Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin:Ktor如何将文本响应为html_Kotlin_Ktor - Fatal编程技术网

Kotlin:Ktor如何将文本响应为html

Kotlin: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

我想使用library
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是什么?一个我必须首先实现的变量?