Optional url segment
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!!
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?
I'll pick this up.
@benjaminhowarth1 Any chance?
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.
Any news on that?