dotnet-websharper / core

WebSharper - Full-stack, functional, reactive web apps and microservices in F# and C#
https://websharper.com
Apache License 2.0
593 stars 52 forks source link

A wildcard endpoint should not require trailing elements #1330

Closed rbauduin closed 1 year ago

rbauduin commented 1 year ago

Defining this endpoint;

    | [<EndPoint "/admin";Wildcard>] Admin of adminPage:string*args:list<string>

Will require the presence of arguments. So it will handle /admin/user/5 but not admin/users. To handle this last path I need to define an additional endpoint:

    | [<EndPoint "/admin";>] AdminWithoutArgs of adminPage:string

It would be great if the first endpoint matched /admin/users too, giving an empty list of trailing elements.

This would help when embedding a sitelet handling admin pages under the app's main sitelet (which is my use-case here)

granicz commented 1 year ago

This largely by design, although I agree it's not the most ideal. I'd just make your endpoint take a single wildcard argument:

| [<EndPoint "/admin"; Wildcard>] Admin of args:list<string>

and pattern match on that:

    open WebSharper.Sitelets.UrlHelpers

    [<Website>]
    let Main =
        Application.MultiPage (fun ctx endpoint ->
            match endpoint with
            | EndPoint.Admin(["users"]) ->
                 Content.Text "Users page"
            | EndPoint.Admin(["user"; INT i]) ->
                 Content.Text $"User's item {i} page"
           ...
rbauduin commented 1 year ago

Thanks for the suggestion, I have adopted it in my code. I just don't understand the INT in | EndPoint.Admin(["user"; INT i]) ->. If it's not a typo it's something I don't know.

granicz commented 1 year ago

It's an active pattern defined in UrlHelpers for extracting an integer from a string - a TryParse, essentially.