Uri Hendler - 258 - Jun 13, 2014

I have a DTO like this, and want to auto-generate routes:
[Route("/{EntityType}/statuses", “GET”)]
public class EntityStatusRequest : IReturn<EntityStatusResponse>
{
    public string EntityType { get; set; }
}

I want to auto-generate routes that look like:
/leads/statuses
/quotes/statuses
/deals/statuses
etc…

The reason for doing this is that I also have DTOs like:
[Route("/leads/{Id}", “GET”)]
public class LeadRequest : IReturn<LeadResponse>
{
    public int Id { get; set; }
}

If I send a GET request to /leads/statuses, ServiceStack tries to bind it to the LeadRequest DTO (which obviously fails).

I can easily generate the routes with this code:
public static void RegisterEntityRoutes(this IAppHost appHost, params Assembly[] assemblies)
{
    foreach (var assembly in assemblies)
    {
        foreach (var entityRouteDtoType in assembly.GetTypes()
        .Where(t => Attribute.IsDefined(t, typeof(EntityRouteAttribute))))
        {
            var attr =  entityRouteDtoType.FirstAttribute<EntityRouteAttribute>();

            foreach (var entityType in entityTypeMap)
            {
                appHost.Routes.Add(
                    entityRouteDtoType,
                    attr.Path.Replace("{EntityType}", entityType.Name),
                    attr.Verbs,
                    attr.Summary.Replace("{EntityType}", entityType.Name),
                    attr.Notes);
            }
        }
    }
}

(The EntityRouteAttribute has the same structure as RouteAttribute.)

This will correctly bind a request to /leads/statuses to the EntityStatusRequest DTO, but the EntityType property will not be populated.

Is there any way to populate this property without having to do custom model binding for the EntityStatusRequest DTO?
The idea is for there to be a lot more DTOs that have a route like /{EntityType}/{OtherParamsHere*}.

You can try giving a lower priority to:
[Route("/leads/{Id}", Priority=-1)]

Or a higher priority to:

appHost.RestPaths.Add(new RestPath(
  entityRouteDtoType,
  attr.Path.Replace("{EntityType}", entityType.Name),
  attr.Verbs,
  attr.Summary.Replace("{EntityType}", entityType.Name),
   attr.Notes)
{
    Priority = 1
});

Uri Hendler:

Thanks for the suggestion, but it didn’t work.

I ended up adding a global typed filter for all DTO types that have the EntityRoute attribute, which parses the request’s path info and populates the EntityType property from there.