views:

277

answers:

3

I understand that TempData is designed to work only between a single page request. But I think have a situation I think breaks the intended functionality.

I use Controllers in preference to Handlers for delivering images. I don't now if that is best practice, but it works very well for me. The problem though, is that each call to one of the image Actions obviously uses up a TempData credit.

Is there a way in MVC to say "This Controller/Action is out of the scope of normal page requests" and therefore either persist the TempData or remove itself from the TempData life cycle completely?

Rich

A: 

I'm not sure of the exact answer to this as I'm pretty new to MVC myself. However, I have heard of people having trouble with AJAX requests killing off the TempData before they wanted it to as well.

I guess you could implement your own Session based system to store this information?

I'm sure someone else will have a more complete answer for you soon, though!

Chris Roberts
I'm adding Image Controllers to a workflow that already uses TempData, I'd prefer not to rock the boat.For this reason, I'm working on an attribute which persists TempData. I know this goes against the purpose of TempData, but I view these Helper Controllers as special cases.Thanks for your help.
kim3er
A: 

I do not completely understand your way of doing it and why but I would rather go with handlers. Or if you stick to controller actions, I can say that this worked for me fine without any issues for delivering images on-the-fly and I used FileContentResult in my controller actions.

Like this:

        return File(imageBytes, imageType);

You get the bytes out of datastore or somewhere..

hope it helps

mare
The Image Action is working, it is the Action's impact on TempData that I'm interested in.
kim3er
+1  A: 

My solution has been to create an Attribute that persists the TempData across page requests. My initial reaction to this is "yuck", but I want to effectively exclude any controllers decorated with the Attribute from the TempData lifecycle.

using System.Web.Mvc;

namespace K3R.Web.Mvc.Filters {
    public sealed class PersistTempDataAttribute : ActionFilterAttribute {
     public PersistTempDataAttribute() { }

     public override void OnActionExecuting(ActionExecutingContext filterContext) {
      var tempData = filterContext.Controller.TempData;
      if (tempData == null || tempData.Count == 0)
       return;

      string[] keys = new string[tempData.Keys.Count];
      tempData.Keys.CopyTo(keys, 0);
      foreach (var key in keys)
       tempData[key] = tempData[key];
     }
    }
}

Any feedback on a better solution would be appreciated.

Rich

kim3er
I don't think completely exclude a controller form the lifecycle is possible.You can apply your PersistTempDataAttribute either on the whole Controller or on a single Method. Remember that the "OnActionExecuting" method is executed >before< your actual function and the "OnActionExecuted" methid is executed >after< your function.However, storing Information beyond a single request should better be stored in a database or any other caching system.
Sebastian Sedlak
Absolutely, I completely agree. I just didn't want to upset existing code that uses TempData with the inclusion of the Images Controller. I concede that using a separate Controller to delivery auxiliary items like images is probably not the best option in this scenario.
kim3er
It looks like MVC2 has now changed the way TempData to works. It now persists until the data is used.
kim3er