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.