首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何路由到一个目录?

我如何路由到一个目录?
EN

Stack Overflow用户
提问于 2014-02-21 03:54:36
回答 1查看 45关注 0票数 2

我正在使用afBedSheet,并希望处理目录中的所有请求。例如对/abcd的请求调用abcdMethod#doSomething

我把路线设为

代码语言:javascript
复制
@Contribute { serviceId="Routes" }
static Void contributeRoutes(OrderedConfig conf) {
  conf.add(Route(`/abcd/?`, abcdMethod#doSomething))
}

然而,当我浏览到/abcd时,我得到了404个错误:(

我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-21 08:31:52

确保您的路由处理程序方法doSomething()不带任何参数。例如,将以下内容保存为Example.fan

代码语言:javascript
复制
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"])
  }
}

然后用以下方式运行:

代码语言:javascript
复制
> fan Example.fan -env dev

(附加-env dev将在404页上列出所有可用的路由。)

因为/abcd/?有一个尾随的?,所以它将匹配http://localhost:8080/abcd的文件URL和http://localhost:8080/abcd/的目录URL。但是请注意,它将与/abcd中的任何URL不匹配。

若要匹配/abcd中的文件,请向路由方法中添加一个Uri参数(以捕获路径),并将路由更改为:

代码语言:javascript
复制
/abcd/**  only matches direct descendants --> /abcd/wotever

/abcd/*** will match subdirectories too   --> /abcd/wot/ever

例如:

代码语言:javascript
复制
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"])
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21924962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档