2

The short version of all this is that I'm using TempData to store feedback messages and after switching to using CookieTempDataProvider they don't show up when using the a PRG pattern, but if I revert back to SessionTempDataProvider they do.

In my MVC4 application I have a pattern of writing an alert message to the temp data for displaying on the screen after a successful Create/Update or when something goes wrong on certain pages and I need to show a failure message. For example...

[HttpPost, ValidateAntiForgeryToken]
public ActionResult Edit(PeopleEditFormModel model)
{
    if (!ModelState.IsValid)
        return RedirectToAction("Edit", new { id = model.ObjectId });
    try
    {
        var person = personService.UpdatePerson(model); // personService is set via DI in constructor
        TempData["message"] = "Person successfully updated.";
        TempData["messageType"] = "success"
        return RedirectToAction("Details", new { id = person.ObjectId });
    }
    catch (DomainException ex)
    {
        //For putting business logic errors in the ModelState
        ModelState.AddModelError(string.Empty, ex.Message);
        return RedirectToAction("Edit", new {id = model.ObjectId});
    }
}

I have a partial view called by my _Layout.cshtml file that is basically a placeholder DIV element (plus some js) that gets filled with my feedback message, and this works fine.

UpdateFeedback

Now this works by default but TempData relies on the session and I'd like to disable it if possible and use a different implementation of the TempDataProvider to get around not having a session. I have to use a different TempDataProvier because if I disable the session without changing the imlementation of TempData, the first time I try to use it I'll get an InvalidOperationException stating

The SessionStateTempDataProvider class requires session state to be enabled.

<system.web>
  <sessionState mode="Off"/>
</system.web>

I've registered a new component with my Dependency Injector, StructureMap, and it looks like this.

//I have tried this with and without the HttpContextScoped()
For<ITempDataProvider>().HttpContextScoped().Use<CookieTempDataProvider>();
For<HttpContextBase>().HttpContextScoped().Use(x => new HttpContextWrapper(HttpContext.Current));

I should mention my implementation of the CookieTempDataProvider is from here Apparently it's been removed.

Now, This solves the exception when I try to use TempData now but my feedback doesn't show up. I thought maybe I broke something in my efforts so I commented out my DI lines and changed my session state mode back to InProc, and tested it. My feedback shows up as expected (after restarting IISExpress). I uncomment my injection lines, change session state to Off, and I'm back to no feedback.

I did some further testing and found that if I do the following in my action I get no message

TempData["message"] = "Person successfully updated.";
TempData["messageType"] = "success"
return RedirectToAction("Details", new { id = person.ObjectId });

But if I ignore the PRG pattern, and instead do this then I see my feedback.

TempData["message"] = "Person successfully updated.";
TempData["messageType"] = "success"
return View("Edit", person);

So my question is, is there a flaw with the commonly accepted CookieTempDataProvider or am I doing something wrong?

Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
  • Maybe help you: http://stackoverflow.com/questions/3809379/asp-net-mvc-tempdata-in-browser-cookie – Felipe Oriani Feb 05 '13 at 16:53
  • I've looked at that question (as well as a few others) and also searched Google but none of the issues seemed to be related to my problem of inconstant behavior when applying PRG pattern. I had assumed that the CookieTempDataProvider included with the MvcContrib (which I believe is what that source is for) library would be fully functional but I wanted to verify if it was the provider or my usage of it that was giving this behavior before I abandoned it in favor of trying to re-invent the wheel. I can't imagine I'm the only person to encounter this problem? – Nick Albrecht Feb 06 '13 at 15:30
  • I'm using the exact same pattern, but I use this implementation of the cookietempdataprovider: https://github.com/brockallen/CookieTempData Works fine for me. – Robin van der Knaap Sep 07 '13 at 10:31

0 Answers0