O'Reilly logo
  • Ryan Garrett thinks this is interesting:

using System.Web.Mvc;
using SportsStore.Domain.Entities;

namespace SportsStore.WebUI.Infrastructure.Binders {

    public class CartModelBinder : IModelBinder {
        private const string sessionKey = "Cart";

        public object BindModel(ControllerContext controllerContext,
            ModelBindingContext bindingContext) {


Cover of Pro ASP.NET MVC 5, Fifth Edition


This shows the proper way to use session state in ASP.NET MVC. You use a custom model binder to register the session objects. That way your action methods can simply specify a type parameter and it will resolve correctly. With the dependency resolver and custom session model binder, the action methods and controllers should be completely parameter based without requiring direct access to a container or the Session object