title: Session Variables Lost Between Controllers & Action Methods
body: <p>I have almost exactly the same scenario described by Nathon Taylor in <a href="https://stackoverflow.com/questions/1258422/asp-net-mvc-sharing-session-state-between-controllers">ASP.NET MVC - Sharing Session State Between Controllers</a>. The problem is that if I save the path to the images inside a Session variable <code>List&lt;string&gt;</code> it is not being defined back in the ItemController so all the paths are being lost... Here's my setup:</p>
  
  <p>Inside ImageController I have the Upload() action method:</p>
  
  <pre><code>    public ActionResult Upload()
      {
          var newFile = System.Web.HttpContext.Current.Request.Files["Filedata"];
          string guid = Guid.NewGuid() + newFile.FileName;
          string itemImagesFolder = Server.MapPath(Url.Content("~/Content/ItemImages/"));
          string fileName = itemImagesFolder + "originals/" + guid;
          newFile.SaveAs(fileName);
  
          var resizePath = itemImagesFolder + "temp/";
          string finalPath;
          foreach (var dim in _dimensions)
          {
              var resizedPath = _imageService.ResizeImage(fileName, resizePath, dim.Width + (dim.Width * 10/100), guid);
              var bytes = _imageService.CropImage(resizedPath, dim.Width, dim.Height, 0, 0);
              finalPath = itemImagesFolder + dim.Title + "/" + guid;
              _imageService.SaveImage(bytes, finalPath);
          }
          AddToSession(guid);
          var returnPath = Url.Content("~/Content/ItemImages/150x150/" + guid);
          return Content(returnPath);
      }
  
      private void AddToSession(string fileName)
      {
          if(Session[SessionKeys.Images] == null)
          {
              var imageList = new List&lt;string&gt;();
              Session[SessionKeys.Images] = imageList;
          }
          ((List&lt;string&gt;)Session[SessionKeys.Images]).Add(fileName);
      }
  </code></pre>
  
  <p>Then inside my ItemController I have the New() action method which has the following code:</p>
  
  <pre><code>        List&lt;string&gt; imageNames;
          var images = new List&lt;Image&gt;();
          if (Session[SessionKeys.Images] != null) //always returns false
          {
              imageNames = Session[SessionKeys.Images] as List&lt;string&gt;;
              int rank = 1;
              foreach (var name in imageNames)
              {
                  var img = new Image {Name = name, Rank = rank};
                  images.Add(img);
                  rank++;
              }
          }
  </code></pre>
  
  <p>Ok so why is this happening and how do I solve it? </p>
  
  <p>Also, I was thinking of whether I could move the ActionMethod that takes care of the upload of the images into the ItemController and store the image paths inside a List property on the ItemController itself, would that actually work? Note though, that images are being uploaded and taken care of via an AJAX request. Then when the user submits the item entry form, all the data about the Item along with the images should be saved to the database...</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I've updated the code. Also I think I should add that I'm using StructureMap as my controller factorory. Could it be a scoping issue? What is the default scope that is usually used by StructureMap?</p>
  
  <pre><code>public class StructureMapDependencyResolver : IDependencyResolver
  {
      public StructureMapDependencyResolver(IContainer container)
      {
          _container = container;
      }
  
      public object GetService(Type serviceType)
      {
          if (serviceType.IsAbstract || serviceType.IsInterface)
          {
              return _container.TryGetInstance(serviceType);
          }
          else
          {
              return _container.GetInstance(serviceType);
          }
      }
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          return _container.GetAllInstances&lt;object&gt;()
  
              .Where(s =&gt; s.GetType() == serviceType);
      }
  
      private readonly IContainer _container;
  }
  </code></pre>
  
  <p>And inside my Global.asax file:</p>
  
  <pre><code>    private static IContainer ConfigureStructureMap()
      {
          ObjectFactory.Configure(x =&gt;
          {
              x.For&lt;IDatabaseFactory&gt;().Use&lt;EfDatabaseFactory&gt;();
              x.For&lt;IUnitOfWork&gt;().Use&lt;UnitOfWork&gt;();
              x.For&lt;IGenericMethodsRepository&gt;().Use&lt;GenericMethodsRepository&gt;();
              x.For&lt;IUserService&gt;().Use&lt;UsersManager&gt;();
              x.For&lt;IBiddingService&gt;().Use&lt;BiddingService&gt;();
              x.For&lt;ISearchService&gt;().Use&lt;SearchService&gt;();
              x.For&lt;IFaqService&gt;().Use&lt;FaqService&gt;();
              x.For&lt;IItemsService&gt;().Use&lt;ItemsService&gt;();
              x.For&lt;IMessagingService&gt;().Use&lt;MessagingService&gt;();
              x.For&lt;IStaticQueriesService&gt;().Use&lt;StaticQueriesService&gt;();
              x.For &lt; IImagesService&lt;Image&gt;&gt;().Use&lt;ImagesService&gt;();
              x.For&lt;ICommentingService&gt;().Use&lt;CommentingService&gt;();
              x.For&lt;ICategoryService&gt;().Use&lt;CategoryService&gt;();
              x.For&lt;IHelper&gt;().Use&lt;Helper&gt;();
              x.For&lt;HttpContext&gt;().HttpContextScoped().Use(HttpContext.Current);
  
              x.For(typeof(Validator&lt;&gt;)).Use(typeof(NullValidator&lt;&gt;));
  
              x.For&lt;Validator&lt;Rating&gt;&gt;().Use&lt;RatingValidator&gt;();
              x.For&lt;Validator&lt;TopLevelCategory&gt;&gt;().Use&lt;TopLevelCategoryValidator&gt;();
          });
  
          Func&lt;Type, IValidator&gt; validatorFactory = type =&gt;
          {
              var valType = typeof(Validator&lt;&gt;).MakeGenericType(type);
              return (IValidator)ObjectFactory.GetInstance(valType);
          };
  
          ObjectFactory.Configure(x =&gt; x.For&lt;IValidationProvider&gt;().Use(() =&gt; new ValidationProvider(validatorFactory)));
          return ObjectFactory.Container;
      }
  </code></pre>
  
  <p>Any thoughts?</p>