3

sorry this is such a long post

I am using F# and WebSharper (I am new to both technologies)

I have some endpoints defined (I have working code until I add the NotFound endpoint)

type EndPoint =
    | [<EndPoint "/">] Home
    | [<EndPoint "/login">] Login
    | [<EndPoint "/about">] About
    | [<EndPoint "/logout">] Logout

    // trying to make a catch-all page handler
    | [<EndPoint "/"; Wildcard>] NotFound of string 
...
let HomePage ctx =
    Templating.Main ctx EndPoint.Home "Home" [
        // page stuff
    ]

let LoginPage ctx =
    Templating.Main ctx EndPoint.Login "Login" [
        h1 [] [text "Login Here"]
        div [] [client <@ Client.LoginWidget() @>]
    ]

// other page constructs
let MissingPage ctx path =
    Templating.Main ctx EndPoint.About "Page Not Found" [
        h1 [] [text "404"]
        p [] [text "The requested page could not be found"]
        p [] [text path]
    ]
...
[<Website>]
let Main =
    Application.MultiPage (fun ctx endpoint ->
        match endpoint with
        | EndPoint.Home -> HomePage ctx
        | EndPoint.About -> AboutPage ctx
        | EndPoint.Login -> LoginPage ctx
        | EndPoint.Logout -> 
            async {
                // call server-side code to log the user out
                // what would i do here to redirect the user to the /login 
                // page
            }
        | EndPoint.NotFound path -> MissingPage ctx path
    )

Add the NotFound endpoint messes up my other pages, for example, my home page starts getting handled by the MissingPage handler. which I can understand since the home page is set to match "/" and the not pattern is matching "/" wildcard, although I would have expected the single / to have matched the Home endpoint and anything else other than /Login /About and /Logout to have match the NotFound branch. But clearly, I do not understand something correctly.

So How can I get a "catch-all" type endpoint so that I can properly handle any path that is not explicitly catered for

the other thing that is messing up when I had the NotFound handling code is the Login handler no longer processes

div [] [client <@ Client.LoginWidget() @>]

And lastly, in the Logout handler, I want to call some server-side code (no problem) but what should I do to then redirect to a new web page, for example, to send the user back to the /login page?

Sorry again for such a long post Derek

Derek Lavine
  • 103
  • 8

1 Answers1

3

Below is based on what Loïc at websharper.com passed on to me, adding here in case it is helpful to someone else.

First Web.config needed

<httpErrors errorMode="Custom">
  <remove statusCode="404"/>
  <error statusCode="404" responseMode="ExecuteURL" path="/notfound"/>
</httpErrors>

type EndPoint = 
... other end points
| [<EndPoint "/notfound"; Wildcard>] NotFound of string


[<Website>]
let Main =
    Application.MultiPage (fun ctx endpoint ->
        match endpoint with
        // handle other endpoints
        // Handle none existant paths
        | EndPoint.NotFound _ ->
            // Parse the original URI from ASP.NET's rewrite, in case you need it
            let requestedUri =
                let q = ctx.RequestUri.Query
                let q = q.[q.IndexOf(';') + 1 ..]
                match System.Uri.TryCreate(q, System.UriKind.Absolute) with
                // The request was to /notfound/... directly
                | false, _ -> ctx.RequestUri
                // The request was to a non-existent path, and rewritten by ASP.NET
                | true, uri -> uri

          Content.Text (sprintf "Unknown URI: %A" requestedUri)
          |> Content.SetStatus Http.Status.NotFound

...
Derek Lavine
  • 103
  • 8