ServiceRunner CustomRoute attribute to change view and bypass dto's execution

My goal is to be able to return a view when the browser requests a route but not execute the get method for the dto. The reason is that the page will be executing a get to the same route via ajax (with content-type json) to retrieve the data. The other piece to this is that I may need to assign different views per get for the same dto because maybe an edit is different than a view page as a simple example. All the pieces seem to work and am asking if there is a more streamlined way to achieve this.

I am creating a CustomRoute attribute that inherits from the Route attribute and adding a public property called View. I want to be able to do something like this:

[CustomRoute("/our-work/{company}")]
[CustomRoute("/our-work/{company}/{slug}", View="workgallery")]
[CustomRoute("/our-work/{company}/{slug}/edit", View ="workgalleryedit")]
public class WorkGallery : TrackPage {}

In the execute method of a custom service runner:

  public override object Execute(IRequest request, object instance, T requestDto)
    {
        if (request.Verb == "GET" && request.ResponseContentType=="text/html" && requestDto.GetType().HasAttribute<CustomRoute>())
        {
            var r = request.GetRoute();
            var route = requestDto.GetType().AllAttributes<CustomRoute>().FirstOrDefault(x => x.Path == r.Path);
            if (route!=null && !String.IsNullOrEmpty(route.View))
            {
                request.Items.Add("View", route.View);
            }
           // I do not want to process this via the dto method just return its view. I may need to add another bypass property to the custom route to control this.
            return requestDto;
        }
        return base.Execute(request, instance, requestDto);
    }

Seems to work well. Any thoughts?

PS. Kudos to the SS team for making this even possible and relatively easy.

You can do it that way although my preference would be to have different Services for the different behavior/views you want as IMO relying on hidden “clever” behavior like this which changes the natural/default behavior is unintuitive for any other developer that needs to work on the project.

Haha, then don’t give us so many clever ways to do stuff ! :smile:

I understand but ideally I just wanted to bypass the execution of the dto method b/c the view setups up a vuejs component that does all the work. so /work-gallery returns the view in the browser and/work-gallery with a get content-type returns the json. I’ll think about what you wrote.