I just published a bug fix (WebSharper 3.6.18) which allows you to use the Wildcard attribute for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
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
    )

Note though that this will catch everything, even URLs to files, so for example if you have client-side content then urls like /Scripts/WebSharper/*.js will not work anymore. If you want to do that, then you'll need to drop to a custom router:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 req.Uri.AbsolutePath))
            (function
                | EndPoint.AnythingElse path -> Some (System.Uri(path))
                | a -> Main.Router.Link a)
    }
By on 9/28/2016 2:12 AM ()

Similarly, I was attempting for single catch-all route. Came up with something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
type Action =
      | Url of string

let MySitelet =
    {
        Router =
            let route (req:Http.Request) =
                // how to get Context/ctx value here?
                let path = req.Uri.AbsolutePath
                if path.StartsWith "/Scripts/" || path.StartsWith "/Content/" then
                    None
                else
                    Some ( Url req.Uri.OriginalString )

            let link  = 
                function
                | Url url -> 
                	// how to get Context/ctx value here?
                	Some (System.Uri(url))
            Router.New route link

        Controller =
            { Handle = function
                | Url url ->
                	// how to get Context/ctx value here?
                	Content.Text("Requested Url "+ url) |> Async.RunSynchronously 
            }
    }

Still searching in getting access to Context/ctx value in custom Router and Controller as commented. Any suggestions while am digging it further?

By on 12/10/2016 11:16 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper