title: Error 500 when using TempData in .NET Core MVC application
body: <p>Hello i am trying to  add an object to <code>TempData</code> and redirect to another controller-action. I get error message 500 when using <code>TempData</code>.</p>
  
  <pre><code>public IActionResult Attach(long Id)
  {
      Story searchedStory=this.context.Stories.Find(Id);
      if(searchedStory!=null)
      {
          TempData["tStory"]=searchedStory;  //JsonConvert.SerializeObject(searchedStory) gets no error and passes 
  
          return RedirectToAction("Index","Location");
      }
      return View("Index");
  }
  
  
  public IActionResult Index(object _story) 
  {             
      Story story=TempData["tStory"] as Story;
      if(story !=null)
      {
      List&lt;Location&gt; Locations = this.context.Locations.ToList();
      ViewBag._story =JsonConvert.SerializeObject(story);
      ViewBag.rstory=story;
      return View(context.Locations);
      }
      return RedirectToAction("Index","Story");
  }
  </code></pre>
  
  <p>P.S Reading through possible solutions I have added  <code>app.UseSession()</code> to the <code>Configure</code> method and <code>services.AddServices()</code> in the <code>ConfigureServices</code> method but to no avail.Are there any obscure settings i must be aware of?</p>
  
  <p>P.S adding <code>ConfigureServices</code> and <code>UseSession</code></p>
  
  <p><strong>ConfigureServices</strong></p>
  
  <pre><code> public void ConfigureServices(IServiceCollection services)
              {
                  services.AddOptions();
                  services.AddDbContext&lt;TreasureContext&gt;(x=&gt;x.UseMySql(connectionString:Constrings[3]));
                  services.AddMvc();
                  services.AddSession();
              }
  </code></pre>
  
  <p><strong>Configure</strong></p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  app.UseExceptionHandler("/Home/Error");
              }
  
              app.UseStaticFiles();
              app.UseSession();
  
              app.UseMvc(routes =&gt;
              {
                  routes.MapRoute(
                      name: "default",
                      template: "{controller=Home}/{action=Index}/{id?}");
              });
          }
  </code></pre>