Routes and default values


We have a service operations to retrieve a task by the task id. We would like to use the same GET method to retrieve nested details or just basic details. Instead of providing a boolean field in the request DTO that the user provides, is there a way to use routes and default values to achieve this?

for eg. tasks/nested/{taskId} and tasks/{taskId} - I can retrieve the request url and check for “nested” and decide whether to include the nested details.

But is there some way where tasks/nested/{taskId} would translate to a boolean field being set to true in the request DTO?

Any suggestions?

Appreciate your help.


I don’t really understand what you’re after, calling this route will populate the TaskId property with the Route value of that segment, if you wanted something else you can change TaskId to a computed property and have set some other boolean property you want.

If you just wanted to know which Route was matched for that Request you can get it from IRequest.GetRoute() extension method, e.g:

var route = request.GetRoute();
var path = route.Path; // /tasks/nested/{TaskId}

Thanks Demis.

I understand this - My question was more related to whether there was some way that the presence of “nested” in the url would translate to a bool property without checking for the text “nested” - for e.g. some sort of default parameter that the user does not need to see. for e.g. url tasks/nested would set a property to true vs. tasks/ would leave the property as false.

Even something like /nestedtasks/{TaskId} would mean IsNested in the request DTO is true but /tasks/{TaskId} would mean IsNested is false.

Also, is there a way to have the code request.GetRoute() in the request DTO?

I may be asking for too much here :slight_smile:

I still don’t really get it, /nestedtasks/{TaskId} and /tasks/{TaskId} are 2 different Routes which can be on 2 different Request DTOs, so based on which Request DTO is called will determine which Route was used otherwise if you wanted to use the same Request DTO you’d need to check base.Request.PathInfo for the /path/info or base.Request.GetRoute().Path for the route.

You can have the IRequest context injected in the Request DTO by implementing IRequiresRequest but as it prevents the Request DTO from being serializable in most serializers I’d recommend just accessing it in your Service class using base.Request.

Thank you. got it now.