是否有一种简单的方法可以使用Sitelets
和Application.MultiPage
来生成一种“默认”路由(例如,捕获“未找到”路线)?
type EndPoint =
| [<EndPoint "/">] Home
| [<EndPoint "/about">] About
[<Website>]
let Main =
Application.MultiPage (fun ctx endpoint ->
match endpoint with
| EndPoint.Home -> HomePage ctx
| EndPoint.About -> AboutPage ctx
我想定义一个EndPoint
来处理除"/home"
和"/about"
之外的任何请求。
答案 0 :(得分:2)
我刚刚发布了一个错误修复程序(WebSharper 3.6.18),它允许您使用Wildcard
属性:
type EndPoint =
| [<EndPoint "/">] Home
| [<EndPoint "/about">] About
| [<EndPoint "/"; Wildcard>] AnythingElse of string
[<Website>]
let Main =
Application.MultiPage (fun ctx endpoint ->
match endpoint with
| EndPoint.Home -> HomePage ctx
| EndPoint.About -> AboutPage ctx
| EndPoint.AnythingElse path -> Content.NotFound // or anything you want
)
请注意,这将捕获所有内容,甚至是文件的URL,因此,例如,如果您有客户端内容,那么/Scripts/WebSharper/*.js
之类的网址将不再有效。如果您想这样做,那么您需要转到自定义路由器:
type EndPoint =
| [<EndPoint "/">] Home
| [<EndPoint "/about">] About
| AnythingElse of string
let Main =
Application.MultiPage (fun ctx endpoint ->
match endpoint with
| EndPoint.Home -> HomePage ctx
| EndPoint.About -> AboutPage ctx
| EndPoint.AnythingElse path -> Content.NotFound // or anything you want
)
[<Website>]
let MainWithFallback =
{ Main with
Router = Router.New
(fun req ->
match Main.Router.Route req with
| Some ep -> Some ep
| None ->
let path = req.Uri.AbsolutePath
if path.StartsWith "/Scripts/" || path.StartsWith "/Content/" then
None
else
Some (EndPoint.AnythingElse path))
(function
| EndPoint.AnythingElse path -> Some (System.Uri(path))
| a -> Main.Router.Link a)
}
(从我在WebSharper论坛的答案中复制)