3

I'm currently building an mvc3 w/ servicestack web app. I'm using servicestack credentials authentification and using servicestack sessions.

I'm already aware that I can access the session in the view by retrieving and setting it in the ViewBag this way:

public partial class HomeController : ControllerBase {
    public virtual ActionResult Index() {
        ViewBag.UserSession = base.UserSession;
        return View();
    }   
}

I really don't want to repeat this piece of code in each of my methods and I'am looking for a generic way of accessing the session in my views, any ideas?

Danny Willem
  • 117
  • 1
  • 8

2 Answers2

4

If you have the opportunity to change the base class of your view, you can just copy the code from the base ServiceStack MVC Controller, e.g:

protected object userSession;
protected TUserSession SessionAs<TUserSession>()
{
    return (TUserSession)(userSession ?? (userSession = Cache.SessionAs<TUserSession>()));
}

Where ICacheClient.SessionAs<T> is an Extension method in ServiceStack.ServiceInterface namespace. But if you don't have the opportunity to use a custom base class the fully qualified access to the typed session is:

EndpointHost.AppHost.TryResolve<ICacheClient>().SessionAs<TUserSession>();

Also ensure you have the latest version of ServiceStack installed.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • I don't get it. Maybe I'm missing something about asp.net mvc3 but how I can access the session object from my view then (for example in the layout master page)? Could you provide me a short example? – Danny Willem Sep 25 '12 at 10:45
  • `ServiceStack.WebHost.Endpoints.EndpointHost.AppHost` is a singleton, you can access it from anywhere in your .NET app, i.e. anywhere there's C# - including your views. – mythz Sep 25 '12 at 10:48
  • Yes, at least I knew that :p, I was just wondering how to roll my own base class for my views and I just found something (http://stackoverflow.com/questions/3963834/whats-the-base-class-of-a-razor-view-in-asp-net-mvc3) Thanks anyway! – Danny Willem Sep 25 '12 at 10:50
2

Using servicestack v4 and based on my understanding from this answer

I have the following CustomWebViewPage which allows me to use UserSession in my view:

public abstract class CustomWebViewPage : WebViewPage
{
    private IServiceStackProvider _serviceStackProvider;
    public virtual IServiceStackProvider ServiceStackProvider
    {
        get
        {
            return _serviceStackProvider ?? (_serviceStackProvider =
                new ServiceStackProvider(new AspNetRequest(base.Context)));
        }
    }

    private object _userSession; // Cache locally
    private TUserSession SessionAs<TUserSession>()
    {
        //ServiceStackProvider.SessionAs<T> always gets the Session from the ICacheClient
        return (TUserSession)(_userSession ?? (_userSession = ServiceStackProvider.SessionAs<TUserSession>()));
    }
    //I have strongly-typed as IAuthSession for now, until I see a need to add my own Custom Session
    protected IAuthSession UserSession
    {
        get { return SessionAs<IAuthSession>(); }
    }

    public virtual bool IsAuthenticated
    {
        get { return ServiceStackProvider.IsAuthenticated; }
    }
}

public abstract class CustomWebViewPage<TModel> : WebViewPage<TModel>
{
    //EXACTLY the same as above...
}
Community
  • 1
  • 1
Darren
  • 9,014
  • 2
  • 39
  • 50