Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Tempdata is crashing my application

I'm very new to ASP.NET and am attempting to pass an object between two controllers in a web application I'm making in Visual Studio 2015. The web application is using an ASP.Net 5 Preview Template Web application (if it helps, I think I'm using beta code 7 and I'm not building for DNX Core 5).

The problem I'm having is whenever I try to put anything into the TempData variable, the program seems to crash. For example, in a "Create" method I have:

        [HttpPost]     public ActionResult Create(Query query)     {         switch (query.QueryTypeID)         {             case 1:                 TempData["Test"] = "Test";                 return RedirectToAction("Index", "EventResults");             case 2:                 break;             default:                 break;         }         return View();     } 

In that method, I attempt to add a simple test string under the key "test". When I run the application with that TempData statement in there, I receive an error message stating

An unhandled exception occurred while processing the request.

InvalidOperationException: Session has not been configured for this application >or request. Microsoft.AspNet.Http.Internal.DefaultHttpContext.get_Session()

I have tried going to the Web.config located in the wwwroot element of the project and adding a "sessionState" object into a "system.web" element, but this had no effect on the error.

Any help would be very much so appreciated as I've been looking for solutions for this everywhere. I'm hoping it's something stupid/blindingly obvious that I somehow missed.

like image 677
RMGT Avatar asked Nov 19 '15 20:11

RMGT


People also ask

Is TempData safe?

Yes, TempData is backed by session storage, so if you are in a load balanced environment extra care must be taken when using it (sticky sessions, persistent session state, etc). TempData has been the de-facto choice when using the PRG pattern, and is what it was designed for.

When should we use TempData in MVC?

What is TempData and How to Use in MVC? TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.

Can we use TempData in .NET core?

In the case of Tempdata, the default implementation in Asp.Net core is done by using SessionState. But we can also use tempdata with the implementation of cookie which can be done with the help of CookieTempDataProvider.


1 Answers

In order to use middleware, such as Session, Cache, etc in ASP.NET 5, you have to enable them explicitly.

Enabling session is done by adding the appropriate nuget package in your project.json file's dependencies section (make sure that the package version matches the versions of the other dependencies you have added):

"Microsoft.AspNet.Session": "1.0.0-*" 

and the appropriate session (cache) storage package as well (like the example below; in memory):

"Microsoft.Extensions.Caching.Memory": "1.0.0-*" 

and adding the middleware to dependency resolution in the Startup.cs Service configuration:

public void ConfigureServices(IServiceCollection services) {     services.AddCaching();     services.AddSession(/* options go here */); } 

and adding the middleware to OWIN in the Startup.cs OWIN configuration:

public void Configure(IApplicationBuilder app) {     app.UseSession();     //... 

Make sure that the UseSession comes before the MVC configuration.

like image 171
PHeiberg Avatar answered Sep 22 '22 02:09

PHeiberg