F#Suave的路线/商店/类别/%s /品牌/%s怎么样?

时间:2016-06-03 20:56:22

标签: .net f# routes suave

我无法想象如何为路径设置路由器,如:

/store/category/%s/brand/%s

我有网络商店演示,它适用于简单的网址,但我不知道如何制作更灵活的配置。

这就是我所拥有的:

type StrPath = PrintfFormat<(string -> string),unit,string,string,string>
// How do this?
type Str2Path = PrintfFormat<(string -> string),unit,string,string,string>

let withParam (key,value) path = sprintf "%s?%s=%s" path key value

module Store =
    //Don't know what put here
    let browseBrand = sprintf "/store/category/%s/brand/%s"
    //This work ok
    let browseCategory : StrPath = "/store/category/%s"
// I need to capture query parameters
let browseBrand cat brand = request (fun r ->
    Views.browse(cat brand))

let webPart = 
    localizeUICulture >>
    choose [
        path Path.Store.overview >=> overview
        pathScan Path.Store.browseBrand browseBrand
        pathScan Path.Store.browseCategory browseCategory

2 个答案:

答案 0 :(得分:1)

那怎么样?

// note the string tuple as return value
type Str2Path = PrintfFormat<(string -> string -> string),unit,string,string,(string * string)>

module Store =
    // your path
    let browseBrand : Str2Path = "/store/category/%s/brand/%s"

// again, note the tuple as input
let browseBrand (cat, brand) = request (Views.browse(cat brand))

let webPart = 
    localizeUICulture >>
    choose [
        pathScan Store.browseBrand browseBrand
        // ... OMMITED
    ]

答案 1 :(得分:0)

我打赌您在显式键入{% block content %} <div class="thing" id="wrapper"> {% for image in post.meta('images') %} <div> <img src="{{ Image(image) }}" /> </div> {% endfor %} </div> <script type="text/javascript"> $(document).ready(function(){ $('.thing').slick({ arrows: true, }); }); </script> {% endblock %} 时会做什么,以便您可以像我一样使用相同的格式字符串来构建和使用url路径。

查询参数似乎不适用于PrintfFormat<_,_,_,_>的网址,以下是一些适用于pathScan的东西

pathScan

关于let clientEvent clientId = sprintf "/client/%i/event" clientId let summary eventId = sprintf "/event/%i/summary" eventId // you can use units of measure in your format strings let getEventValidation () : PrintfFormat<int<EventId> -> _,_,_,_,int<EventId>> = "/event/%i/validation" let checkoutUploaded () : PrintfFormat<int<CheckoutId> -> _ -> _ ,_,_,_,_> = "/checkout/%i/uploaded/username/%s" let getEventDownloadNoQuery () : PrintfFormat<int<EventId> -> _,_,_,_,_> = "/event/%i/download" let userName="userName" let tabletIdent = "tabletIdent" let getEventDownload () : PrintfFormat<int<EventId> -> _ -> _ -> _,_,_,_,_> = "/event/%i/download?userName=%s&tabletIdent=%s" // we can use the actual format string as the method/variable name // is it a good idea? not sure. // get participant for edit let ``get /participant/%i``() : PrintfFormat<int<ParticipantId> -> _,_,_,_,int<ParticipantId>> = "/participant/%i" let getUsers = "/user" // we can include the action in the variable name too // also questionable but possibly useful let ``post /participant`` = "/participant" let ``get /client/%i/participant`` () : PrintfFormat<int<ClientId> -> _,_,_,_,int<ClientId>> = "/client/%i/participant" let ``get /event/%i/participants`` () : PrintfFormat<int<EventId> -> _,_,_,_,int<EventId>> = "/event/%i/participants" let resultList clientId pId = sprintf "/result/client/%i/participant/%i" clientId pId 的通知,我必须有2条不同的路径,一条用于客户端生成正确的url,另一条用于服务器。真烂。

下面是一个与上述示例无关的示例webPart:

getEventDownload

就查询参数而言,我认为最好让路径匹配,并为缺少查询参数或类似内容返回无效的请求消息。

当然,您可以在pathScan "/client/%i/customer/%i" (fun (clientId,customerId) -> sprintf "Customer %i, Customer %i" clientId customerId |> OK)匹配处理程序中进行分支。

处理查询参数的示例:

pathScan

let serveResult cn :WebPart = fun ctx -> let eventIdField = toCamelCase RMeta.EventId let pIdField = toCamelCase RMeta.ParticipantId let eventIdOpt = ctx.request.queryParamOpt eventIdField let pIdOpt = ctx.request.queryParamOpt pIdField match eventIdOpt, pIdOpt with | Some(_,Some (ParseInt eventId)), Some(_,Some(ParseInt pId)) -> let model = Dal.DataAccess.Results.getResult cn (1<EventId> * eventId) (1<ParticipantId> * pId) match model with | Some m -> OK (Json.toJson m) // |> Option.getOrDefault' (lazy({ResultRecord.Zero() with EventId = eventId; ParticipantId = pId})) | _ -> RequestErrors.NOT_FOUND ctx.request.rawQuery | _ -> RequestErrors.BAD_REQUEST (ctx.request.rawQuery) |> fun f -> f ctx 个与queryParams组成的示例

WebPart