-
Notifications
You must be signed in to change notification settings - Fork 267
Closed
Labels
documentationRequest to change or extend documentationRequest to change or extend documentationfeature requestRequest to add new functionalityRequest to add new functionality
Description
I think it sounds like a good idea to update the documentation to avoid confusion. I played around and arrived at a working example which I think should be added in some fashion and mentioned in the section for routeBind (since
routeBinddoes not work with endpoint routing?)Also I feel like the
tryBindRoutefunction from the example below is missing from the library?// Program.fs open Microsoft.AspNetCore.Hosting open Microsoft.AspNetCore.Builder open Microsoft.Extensions.Hosting open Microsoft.Extensions.DependencyInjection open Giraffe open Giraffe.EndpointRouting open Microsoft.AspNetCore.Routing open Microsoft.AspNetCore.Http open Microsoft.Extensions.Primitives open System.Collections.Generic [<CLIMutable>] type Person = { FirstName : string LastName : string } let tryBindRoute<'T> (ctx: HttpContext) : 'T option = let routeData = ctx.GetRouteData().Values |> Seq.map (fun kvp -> KeyValuePair(kvp.Key, StringValues(kvp.Value :?> string))) |> fun kvps -> Dictionary<string, StringValues>(kvps) :> IDictionary<string, StringValues> match ModelParser.tryParse<'T> None routeData with | Ok model -> Some model | Error _ -> None let bindRouteHandler<'T> (handler: 'T -> HttpHandler) : HttpHandler = fun next ctx -> match tryBindRoute<'T> ctx with | Some model -> handler model next ctx | None -> RequestErrors.BAD_REQUEST "Failed to bind route parameters" next ctx let personHandler (person : Person) : HttpHandler = sprintf "Hello %s %s" person.FirstName person.LastName |> Successful.OK let endpoints = [ GET [ route "/p/{firstName}/{lastName}" (bindRouteHandler<Person> personHandler) ] ] let configureApp (appBuilder : IApplicationBuilder) = appBuilder .UseRouting() .UseEndpoints(fun e -> e.MapGiraffeEndpoints(endpoints)) |> ignore let configureServices (services : IServiceCollection) = // Add Giraffe dependencies services.AddGiraffe() |> ignore [<EntryPoint>] let main _ = Host.CreateDefaultBuilder() .ConfigureWebHostDefaults( fun webHostBuilder -> webHostBuilder .Configure(configureApp) .ConfigureServices(configureServices) |> ignore) .Build() .Run() 0
Originally posted by @acatuttle in #704
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
documentationRequest to change or extend documentationRequest to change or extend documentationfeature requestRequest to add new functionalityRequest to add new functionality