servicestackservicestack-razor

ServiceStack default Razor view with service


I want to host a very simple razor page inside a self host SS app.

I need the / path to resolve to the default.cshtml - this works out of the box.

But i need to access the user auth session inside the view. To do this I am guessing I need a service to create the model for default.cshtml

Everything I have tried so far doesn't work and I can't create a DefaultRequest with route / as that isn't allowed.

Anyone got any clues as to what I need to do?

I have tried with fall back route but no luck:

[FallbackRoute("/{Path*}")]
public class Fallback
{
    public string Path { get; set; }
}

public class DefaultService : Service
{
    public DefaultService ()
    {
    }

    public object Get(Fallback request){
        return new HttpResult() // #6
        {
            View = "Rockstars"  // #1
        };
    }
}

Solution

  • Your typed UserAuth session is directly accessible in your Razor Views base ViewPageBase with base.SessionAs, e.g:

    @{
        var session = base.SessionAs<CustomUserSession>();
    }
    

    You've also got access to your dynamic session bag with base.SessionBag as well as base.IsAuthenticated to determine if the user is authenticated or not.

    Fallback Route

    In order to invoke a Service to handle your default page you need to use a Fallback Route, e.g:

    [FallbackRoute("/{Path*}")]
    public class DefaultPage
    {
        public string Path { get; set; }
    }
    

    A Fallback Service can be used to handle every unmatched request including the root / page.