People who used TempData in ASP.NET MVC, probably knows that by default TempData is stored in a Session state. This means that the web application must have sessions enabled. Now, the ASP.NET Core 2.0 gives two TempData providers; a Cookie based and a Session State-based. This blog shows how to use both of them in an application in ASP.NET Core.
The data being stored is small in size.
A web application is a part of a web farm and you don’t need sticky sessions.
Since cookie based TempData is used by default you don’t have to do any configuration to use it. Just store and receive the TempData key-value pairs as you do. An example is given here:
public IActionResult Index1()
TempData[“message”] = DateTime.Now;
public IActionResult Index2()
The above code highlights two actions – Index1 and Index2 – of HomeController. The Index1 () action stores a date-time stamp in a dictionary of TempData with a key name of the message. The Index1 view offers a hyperlink to direct to the Home/Index2. The Index2 () action gives back Index2 view. The Index2 view reads the TempData and shows the message to the user.
Index1 view is given below:
@if (TempData.Peek(“message”) == null)
<h2>TempData has not been set.</h2>
<h2>TempData has been set.</h2>
<a asp-controller=”home” asp-action=”index2″>
Go to Index2
As shown the Razor code finds out whether the message has been stored in TempData or not. It does by using Peek () method of TempData. Since Peek() issued, the key won’t be removed from the TempData. And a message gets displayed in the browser mentioning the status of the message key.
An anchor tag helper offers a hyperlink which points to the Index2 () action of HomeController.
When user clicks on the hyperlink given by Index1. The value of message is sent out in the browser. Markup of Index2 is given below.
<a asp-controller=”home” asp-action=”index1″>
Go to Index1
Index2 reads the TempData dictionary and yields the date-time stamp value in the browser. A hyperlink permits the user to go back to the Index1.
How to know that the TempData is really using the cookies. You need to run the application with F12 tools and locate the cookies option.
The cookie value is Base64 encoded. Since cookies are being used to store TempData all the limitations of cookie storage apply like browser support, size etc.
Session State based TempData
Let’s see how to use Session State based TempData and not the cookie based.
Session state based TempData
To allow Session state based TempData storage you must enable sessions using middleware and you also have to change the default provider. The following code shows how to do it.
public void ConfigureServices(IServiceCollection
public void Configure(IApplicationBuilder app,
The ConfigureServices() calls out AddSessionStateTempDataProvider() to alter the default TempData provider to SessionStateTempDataProvider. It also summons AddSession() to allow in-memory session state for the application. The Configure() summons UseSession() to use the session state middleware.
The HomeController and its both the actions, Index1() and Index2() remains unchanged. On running the application, you can see that the final outcome will be same as before. But there won’t be any CookieTempDataProvider cookie. Instead, a Session cookie.
Session state based TempData comes handy when the application is using session state for other reasons.
That’s it for now!
If you want to upgrade yourself to the basics and concepts of Dot Net Course then our institute would be of great help and support in Dot NET training program.
Code source: Online database