Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Regex sub route helper function which passes arguments similar to routef #446

Closed
polomani opened this issue Sep 24, 2020 · 0 comments
Closed

Comments

@polomani
Copy link

polomani commented Sep 24, 2020

Is your feature request related to a problem? Please describe.
There are cases when I would like to not treat / as a sub-routing symbol.
This can be useful for instance in OData functions. Those functions are parts of request path, not a query and hence will not be url-encoded by an http client:

{{my-cool-odata-api}}/Calculate(Expression='1/2 + PI()')

The slash in this expression will not allow me to use routef with /Calculate(%s), since / is a sub-routing separator, so it'll just split my function into 2 pieces.

Describe the solution you'd like
An extra sub route helper, which works similar to routef but uses Regex:

/// **Description**
///
/// Filters an incoming HTTP request based on the request path using Regex (case sensitive).
///
/// If the route matches the incoming HTTP request then the Regex groups will be passed into the supplied `routeHandler`.
/// This is similar to routex but also allows to use matched strings as parameters for a controller.
/// 
/// **Parameters**
///
/// `path`: Regex path.
/// `routeHandler`: A function which accepts a seq<string> of the matched groups and returns a `HttpHandler` function which will subsequently deal with the request.
///
///
let routexp (path : string) (routeHandler : seq<string> -> HttpHandler): HttpHandler =
    let pattern = sprintf "^%s$" path
    let regex   = Regex(pattern, RegexOptions.Compiled)

    fun (next : HttpFunc) (ctx : Microsoft.AspNetCore.Http.HttpContext) ->
        let result  = regex.Match (SubRouting.getNextPartOfPath ctx)
        match result.Success with
        | true  -> 
            let args = result.Groups |> Seq.map (fun x -> x.Value)
            routeHandler args next ctx 
        | false -> skipPipeline
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant