0

I'm having an odd issue with ASP.NET Core MVC.

I'm trying to keep track of a Guid called Uid as I move from page to page in an app at work.

In the controller, I tried setting each of these as a test on my Edit action.

model.Uid = uid.ToString();
TempData["Uid"] = uid.ToString();
ViewData["Uid"] = uid.ToString();

But as I step through the application, when I navigate through the pages, these values are all reset to null when I'd expect them to persist.

I could swear I've used these before and they persisted... any ideas for how I might narrow down the cause?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bleak Morn
  • 139
  • 1
  • 9
  • Try `TempData.Keep("Uid")` to persist the value. You can also use `TempData.Peek("Uid")` to get the value without marking the value for deletion. (Temp data will delete values that have been read). – hijinxbassist Nov 02 '21 at 19:30
  • Does this answer your question? **1)** [TempData persistence](https://stackoverflow.com/questions/45096482/tempdata-persistence) **2)** [ViewBag/ViewData Lifecycle](https://stackoverflow.com/a/9186855/6630084) – Jackdaw Nov 02 '21 at 20:57
  • @hijinxbassist Thanks for pointing that out. I had `TempData.Keep()` - but I changed it to `TempData.Keep("Uid")` and tested. Unfortunately it still isn't persisting the value. This was a solution set up by another dev that I don't have access to. Is it possible that there is some config setting that prevents these values from persisting when they otherwise should? – Bleak Morn Nov 02 '21 at 21:15
  • By default TempData uses the Session to store the values. You can also implement a custom provider. Check which session mode (if any) was chosen by looking for the sessionState node in the web.config. – hijinxbassist Nov 02 '21 at 21:53
  • @hijinxbassist Interesting. Since it is a Core app, it didn't have a web.config - but I added one in the root with the following. `` Unfortunately, when I tested it, the values were still not persisting. – Bleak Morn Nov 03 '21 at 14:01
  • Try this: Inside of `Startup.cs`, inside the `ConfigureServices` function add `services.AddSession();` and inside `Configure` function BEFORE `UseMvc` line, add `app.UseSession();` – hijinxbassist Nov 03 '21 at 17:27
  • @hijinxbassist The only mention of MVC in `Startup.cs` was `services.AddMvc();`. When I added `UseMvc` it gave a warning that I had to set the MVC option `EnableEndpointRouting = false`. I did that as follows: `services.AddMvc(options => options.EnableEndpointRouting = false);` When I tested though, `TempData`, `ViewData`, and `model` values were still not persisting. – Bleak Morn Nov 04 '21 at 18:58
  • You should add `services.AddSession()`, not changing the `AddMvc` call. The second is to enable the session in the app, via `app.UseSession()` inside of the `Configure` function. – hijinxbassist Nov 04 '21 at 20:30
  • @hijinxbassist I did add `services.AddSession()` and `app.UseSession()` as described. When I removed the options from AddMvc I get the following error though. `System.InvalidOperationException HResult=0x80131509 Message=Endpoint Routing does not support 'IApplicationBuilder.UseMvc(...)'. To use 'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices(...). Source=Microsoft.AspNetCore.Mvc.Core` – Bleak Morn Nov 05 '21 at 13:00

0 Answers0