(Go: >> BACK << -|- >> HOME <<)

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

Optional url segment #591

Open
nbsoftware opened this issue Dec 8, 2018 · 4 comments
Open

Optional url segment #591

nbsoftware opened this issue Dec 8, 2018 · 4 comments

Comments

@nbsoftware
Copy link
nbsoftware commented Dec 8, 2018

Would there be a way to support route definitions with optional segments (as for instance in PHP Slim Framework)?

For instance, if I want to delete say one specific notification message from a user/device or delete all of them I would like to have the following RefitDefinition:

(omitting [Header] parameters for simplicity sake)

        [Delete("/push/notifMsg/{deviceId}[/{notifMsgId}]")]
        Task<StatusCountResponse> DeleteNotifMsgs(
            Guid? deviceId,
            Guid? notificationMsgId,
            CancellationToken ct);

Notice the brackets.

The issue right now is that with the only supported definition which is the full one (i.e. without brackets) as:

        [Delete("/push/notifMsg/{deviceId}/{notifMsgId}")]
        Task<StatusCountResponse> DeleteNotifMsgs(
            Guid? deviceId,
            Guid? notificationMsgId,
            CancellationToken ct);

If say notificationMsgId is null, it generates the url as /push/notifMsg/{deviceId}/ i.e. with a trailing slash which gets a 404 response code with php slim-based server code because it is not recognized as a registered valid route.
But /push/notifMsg/{deviceId} is indeed valid on the server side.

Would there be an existing workaround for this use case?

Unless I'm missing the obvious, which is always possible!!

@nbsoftware
Copy link
Author

Of course the syntax I was showing was taken from some PHP framework.
With Microsoft's Web API 2 i.e. using ?, this translates to:

[Delete("/push/notifMsg/{deviceId}/{notifMsgId?}")]
Task<StatusCountResponse> DeleteNotifMsgs(
            Guid? deviceId,
            Guid? notificationMsgId = null,
            CancellationToken ct);

and will issue a call to /push/notifMsg/{deviceId}if notifMsgId isn't provided.
But I'm sure you get the idea.

Anyone?

@jamiehowarth0
Copy link
Member

I'll pick this up.

@nbsoftware
Copy link
Author

@benjaminhowarth1
Any chance?

@RobThree
Copy link

I could also use this. The workaround for this we use now is an overload:

interface MyApi {
  [Get("/foo/{bar}")]
  GetFoo(string bar);

  [Get("/foo/{bar}/{baz}")]
  GetFoo(string bar, string baz);
}

This can then be 'wrapped' in a class which does a string.IsNullOrEmpty(baz) and invokes the correct method. It's not too bad, but having a way to specify an optional path argument would be nice.

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

3 participants