有没有一种简单的方法可以使用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"
以外的任何请求。 最佳答案
我刚刚发布了一个错误修复程序(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
之类的URL将不再起作用。如果要这样做,则需要使用自定义路由器: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论坛中的回答)
关于f# - WebSharper-是否有捕获 "not found"路由的简单方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39670877/