3

申し訳ありませんが、これは非常に長い投稿です

F# と WebSharper を使用しています (どちらのテクノロジも初めてです)

いくつかのエンドポイントが定義されています (NotFound エンドポイントを追加するまで、動作するコードがあります)

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
    )

NotFound エンドポイントを追加すると、他のページが台無しになります。たとえば、ホームページが MissingPage ハンドラーによって処理され始めます。ホームページは「/」に一致するように設定されており、not パターンは「/」ワイルドカードに一致するため、理解できますが、単一の / が Home エンドポイントと /Login /About 以外のものと一致すると予想していました。 /Logout は、NotFound ブランチと一致するようにします。しかし、明らかに、私は何かを正しく理解していません。

では、明示的に提供されていないパスを適切に処理できるように、「キャッチオール」タイプのエンドポイントを取得するにはどうすればよいですか

NotFound 処理コードを持っていたときに台無しになっているもう 1 つのことは、ログイン ハンドラーが処理されなくなったことです。

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

最後に、Logout ハンドラーで、サーバー側のコードを呼び出したいのですが (問題ありません)、新しい Web ページにリダイレクトするにはどうすればよいですか? たとえば、ユーザーを /login ページに戻すにはどうすればよいでしょうか?

このような長い投稿でもう一度申し訳ありません

4

1 に答える 1

3

以下は、websharper.com の Loïc が私に伝えた内容に基づいており、他の人の役に立つ場合に備えてここに追加します。

最初の Web.config が必要

<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

...
于 2018-05-14T04:06:38.957 に答える