0
votes

I am trying to use the CookieTempDataProvider in MVC 3 futures assembly. I believe I have "wired" it up successfully using ninject. Below is the code from my app_start.cs file:

[assembly: WebActivator.PreApplicationStartMethod(typeof(Web.AppStart), "Start")] namespace Web { public static class AppStart { public static void RegisterServices(IKernel kernel) {

        kernel.Bind<ITempDataProvider>().To<CookieTempDataProvider>();
    }

    public static void Start() {
        // Create Ninject DI Kernel 
      //  IKernel kernel = new StandardKernel();
        IKernel kernel = Container;

        // Register services with our Ninject DI Container
        RegisterServices(kernel);

        // Tell ASP.NET MVC 3 to use our Ninject DI Container 
        DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));

    }

    static IKernel _container;
    public static IKernel Container
    {
        get
        {
            if (_container == null)
                _container = new StandardKernel();
            return _container;
        }
    }

However, when I access my page that uses TempData, I get the this error indicating that it is still trying to use the SessionTempDataProvider:

Server Error in '/' Application. The SessionStateTempDataProvider class requires session state to be enabled. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The SessionStateTempDataProvider class requires session state to be enabled.

I must be missing something and I can't figure out what it is. Any help would be most appreciated.

Many Thanks

2

2 Answers

0
votes

I've only been able to get this to work with the BaseController approach. The controller creation processes does't ask Ninject for a ITempDataProvider.

public class BaseController : Controller
{
    protected override ITempDataProvider CreateTempDataProvider()
    {
        return new CookieTempDataProvider(HttpContext);
    }
}
0
votes
  1. Extend the controller class

    public static void SetMessage(this Controller controller, String message)
    {
        controller.TempData["Messag"] = message;
    }
    
  2. Then you can use it like this:

    public ActionResult Save()
    {
        //Validation... 
    
        Save(Foo);
        this.SetMessage("Item saved successfully");
        return Redirect("/Site"); 
    }
    
  3. No number three :)