确保您的路由处理方法doSomething()
确实不带任何参数例如,以下内容作为Example.fan
:
using afIoc
using afBedSheet
class MyRoutes {
Text abcdMethod() {
return Text.fromPlain("Hello from `abcd/`!")
}
}
class AppModule {
@Contribute { serviceId="Routes" }
static Void contributeRoutes(OrderedConfig conf) {
conf.add(Route(`/abcd/?`, MyRoutes#abcdMethod))
}
}
class Example {
Int main() {
afBedSheet::Main().main([AppModule#.qname, "8080"])
}
}
而且随着运行:
> fan Example.fan -env dev
(追加-env dev
将列出404页上的所有可用路由)
因为/abcd/?
一直尾随?
,它将匹配两个文件http://localhost:8080/abcd
的URL和http://localhost:8080/abcd/
的目录URL。但请注意,不会与匹配/abcd
中的任何网址。
要匹配文件里面/abcd
,一个URI参数添加到您的路线方法(捕捉路径),并改变你的路线:
/abcd/** only matches direct descendants --> /abcd/wotever
/abcd/*** will match subdirectories too --> /abcd/wot/ever
例如:
using afIoc
using afBedSheet
class MyRoutes {
Text abcdMethod(Uri? subpath) {
return Text.fromPlain("Hello from `abcd/` - the sub-path is $subpath")
}
}
class AppModule {
@Contribute { serviceId="Routes" }
static Void contributeRoutes(OrderedConfig conf) {
conf.add(Route(`/abcd/***`, MyRoutes#abcdMethod))
}
}
class Example {
Int main() {
afBedSheet::Main().main([AppModule#.qname, "8080"])
}
}