Cookie based TempData Provider in ASP.Net Core

TempData in ASP.NET MVC is used to store temporary data which can be used in the subsequent request. Data stored in TempData will be cleared once it is accessed, we need to call TempData.Keep() explicitly to retain values. The default TempData implementation in ASP.NET MVC is based on Session. In ASP.Net Core, we have a provision to use Cookies as storage for TempData.

NOTE: Updated this tutorial on 2/27/2017 with MSBuild based Dotnet SDK.

Create an ASP.Net Core Application using VS 2017. I am using following version of Dotnet SDK which is based on MSBuild/CSProj (remember that previous versions of ASP.Net Core are based on Project.json/XProj.).

image

Add the following package dependency.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
  <PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
  </ItemGroup>
</Project>

Lets get started by adding Cookie based TempData Provider in ConfigureServices of Startup class. Make sure to add it after AddMvc().

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
}

Now lets change Home controller actions to use TempData.

public class HomeController : Controller
{
    public IActionResult Index()
    {
        TempData["Test"] = "Rami";
        return View();
    }

    public IActionResult About()
    {
        ViewData["Message"] = TempData["Test"];
        return View();
    }
}

Run the application. First visit the Index view. We should see the CookieTempDataProvider cookie generated in Fiddler tool as shown below.

image

Now navigate to About View, we should the value previously stored in TempData. When we look at fiddler response again, the cookie will be present with no value and an old expires date (so that none of the browsers can use it) because we have accessed the value in TempData (and we never used TempData.Keep()).

image

image

That’s it for now, Happy Coding and Stay Tuned!!!

You may also like...