https://kotlinlang.org logo
#ktor
Title
# ktor
h

Hadi

08/08/2021, 8:08 AM
Hello all, I could not find anything on the absolute path in Ktor routing on the internet. Do you know a way one can link to a path generated form absolute top level of the app? e.g.
Copy code
route("") {
    route("/{year}/{month?}/{day?}") {
        get {
            call.respondHtml{
                title { +"title" }
                body {
                    a (href = "$baseUrl/somepath") { +"link to top level" }
                }
            }
        }
    }
}
In this example, if I do
href="/somepath"
, it will go to different places if month or day is given.
a

Aleksei Tirman [JB]

08/09/2021, 9:58 AM
Could you please explain what do you mean by a path generated from the absolute top level of the app?
h

Hadi

08/09/2021, 1:03 PM
Hi Alekseit, thanks for the reply. I wondered if (in the example above) one could use a link created with absolute path in the href. So if you are in the
route("someEndpoint")
and do
a (href="/test")
, you will get a link which is
/someEndpoint/test
for the href. However, if you want to get to
/test
, you need to do
a (href="../test")
. This works in a normal route method. But when your route has optional arguments as above, this will give
/test
,
/year/test
or
/year/month/test
if no month and day is given, month is given or month and day are given, respectively.
What I need is a method that would always point to the baseUrl of the app, namely, when you say
a (href=$baseUrl/test)
it always returns
/test
no matter which route your are currently in.
a

Aleksei Tirman [JB]

08/09/2021, 3:45 PM
When I use
href = "/test"
then a web browser always points me to
<protocol://host>:port/test
no matter what the current URL is, e.g. http://localhost:8080/2020/10/23. Please describe how exactly do you observe such behavior?
👍 1
h

Hadi

08/09/2021, 5:51 PM
Oh I am really sorry. You're actually right. My mistake was that since I have
install(IgnoreTrailingSlash)
, I did not put the leading
/
in the path for
href
. Without the leading slash it follows current path but with that it goes form the base path. Thanks a lot. 👍
2 Views