Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Putting a null value in TempData results in ArgumentNullException on subsequent requests #5243

Closed
DamianEdwards opened this issue Sep 6, 2016 · 3 comments
Assignees

Comments

@DamianEdwards
Copy link
Member

Repro steps

  1. Create a new MVC application

  2. Configure it to use session so that TempData works

    1. Add a reference to Microsoft.AspNetCore.Session
    2. Modify Startup.ConfigureServices to add the session services services.AddSession()
    3. Modify Startup.Configure to add the session middleware app.UseSession()
  3. Modify HomeController.Index to insert a null value into TempData:

    public IActionResult Index()
    {
       TempData["WhatWillThisDo"] = null;
       return View();
    }
  4. Run the app

Expected

The value at the specified key is cleared.

Actual

The first request succeeds. Subsequent requests fail with the following exception:

System.ArgumentNullException was unhandled by user code
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: dictionary
  ParamName=dictionary
  Source=System.Private.CoreLib
  StackTrace:
       at System.Collections.Generic.Dictionary`2..ctor(IDictionary`2 dictionary, IEqualityComparer`1 comparer)
       at Microsoft.AspNetCore.Mvc.ViewFeatures.SessionStateTempDataProvider.LoadTempData(HttpContext context)
       at Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary.Load()
       at Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary.set_Item(String key, Object value)
       at WebApplication3.Controllers.HomeController.Index() in c:\users\dedward\documents\visual studio 2015\Projects\WebApplication3\src\WebApplication3\Controllers\HomeController.cs:line 13
       at lambda_method(Closure , Object , Object[] )
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionFilterAsync>d__28.MoveNext()
  InnerException: 

@DamianEdwards
Copy link
Member Author

OK this is even worse. It seems to happen if you set it to anything other than a value type, e.g. the same thing happens with the following: TempData["WhatWillThisDo"] = new object();

I'm guessing we don't want that to work, but I'm also guessing we don't want it to fail this way.

@rynowak
Copy link
Member

rynowak commented Sep 7, 2016

/cc @ajaybhargavb

@danroth27
Copy link
Member

We should understand why this is blowing up. If this is something that shouldn't be supported then we should throw a better exception. If bug then fix.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants