NOTE: Handling TempData and Session is made easy with extension methods in the BetterSession Nuget package.
ASPNET 5 is designed to be configurable. It starts out with almost nothing and you choose what you need. In previous versions of MVC we got TempData out the box. Not so with the new iteration.

So to enable TempData for MVC you need sessions. In project.json add the following lines to dependencies
{% highlight csharp %} "Microsoft.AspNet.Session": "1.0.0-", "Microsoft.Extensions.Caching.Memory": "1.0.0-" {% endhighlight %}
In Startup.cs the configuration of your services will need the following:
{% highlight csharp %} public void ConfigureServices(IServiceCollection services) { services.AddCaching(); //this is the NB line for this post services.AddSession(o => ); services.AddMvc(); } {% endhighlight %}
While the app builder configuration will be something like so:
{% highlight csharp %} public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); //this is the NB line for this post app.UseSession(); app.UseIISPlatformHandler(); app.UseStaticFiles(); app.UseMvc(); } {% endhighlight %}
Then accessing TempData is done through the dependency injection/service locator:
{% highlight csharp %} public class TempController : Controller { private const string key = "name"; private readonly ITempDataDictionary _tempData;
public TempController(ITempDataDictionary tempData)
public IActionResult Index() { _tempData[key] = "Devon"; return RedirectToAction("Carry"); }
public IActionResult Carry() { return View("Index", _tempData[key]); } } {% endhighlight %}
OR
{% highlight csharp %}
var tempData = HttpContext.RequestServices.GetRequiredService
NOTE 1: When using ITempDataDictionary in a custom ActionResult I needed to mark the class with IKeepTempDataResult for it to work.
NOTE 2: I am not sure if this is going to change but currently the implementation for ITempDataDictionary only accepts primitive values (and string). I got around this by serializing to and from json. If you want to do this, you might find these extension methods useful.
{% highlight csharp %}
public static void SetAsJson
public static T GetFromJson
if(v is T)
{
return (T)v;
}
if(v is string && typeof(T) != typeof(string))
{
var obj = JsonConvert.DeserializeObject<T>((string)v);
return obj;
}
} return default(T); } {% endhighlight %}
So hope you and future me finds this post useful. I am going to try blog little things like this as I work more with ASP.NET 5. Please let me know in the comments below if you did find it useful or if I missed anything. Also let me know if there are other topics you want me to cover.