Damian S
Damian S

Reputation: 213

servicestack Creating a wildcard route to different DTO's

I'm trying to create set of service stack routes that have wildcards in it.
I can't change it as the Url to respond to are defined by another product.

It seems as soon as service stack sees the * in the route, it eats everything to the end of the path?

So all of these example urls seem to get routed as the Catalogue Request, not the View Request in the second case

http://domain/rest/folder1
http://domain/rest/folder1/damian/View

Is it possible to make the smart routing literal weighting detect literals after wildcards?

I guess when its hits a wildcard going left to right, it has to jump to resolving right to left back to the wildcard, and the wildcard is what remains?

For example

[Route("/rest/{Folder*}/{Name}/View")]
public class ViewRequest
{
    public string Folder { get; set }
    public string Name { get; set; }
}

AND

[Route("/rest/{Folder*}")]
public class CatalogRequest
{
    public string Folder { get; set }
}

thanks, Damian

Upvotes: 1

Views: 367

Answers (1)

mythz
mythz

Reputation: 143319

Is it possible to make the smart routing literal weighting detect literals after wildcards?

No. The wildcard must be the last element on the route, which matches the remaining part of the PathInfo into the selected variable, e.g:

This is Valid:

[Route("/rest/{Folder*}")]
public class CatalogRequest { ... }

This is not:

Route("/rest/{Folder*}/{Name}/View")]
public class ViewRequest { ... }

In your service you can still use the value in your service and call a different service based on that logic, e.g:

public object Get(CatalogRequest request)
{
    if (request.Folder.SplitOnLast('/').Last() == "View")
    {
        using (var service = base.ResolveService<ViewService>())
        {
            return service.Get(request.TranslateTo<ViewRequest>());
        }
    }
    ...
}

Upvotes: 1

Related Questions