servicestackservicestack-razor

Custom RazorPage get access to IRequest and Route Info


Using the Razor implementation for ServiceStack and AspNetCore how can I get access to the IRequest to get the route info for a custom razor page? Ultimately I want to get to the Name attribute on the route if possible.

public abstract class CustomView : RazorPage { public IRequest Req { get; set; } // always null

    protected Breadcrumb Breadcrumb
    {
        get
        {
            return new Breadcrumb(this.Req); // need to pass IRequest to breadcrumbs so it can produce them
        }
    }
}

Routes are defined with a custom attribute which inherits from Route.

[CustomRoute("/message/{id}", View = "MessageDetailView", Name = "GetById")]

Custom Route:

 public class CustomRoute : RouteAttribute
    {
        public string Name { get; set; }
        public string View { get; set; }
        public CustomRoute(string path) : base(path) { }
        public CustomRoute(string path, string verbs) : base(path, verbs) { }
    }

Solution

  • It seems that at least in web applications (as opposed to self hosted) the following works:

     IRequest req = HostContext.TryGetCurrentRequest();