I want to refresh the _layout page for updating data in the layout section, when a action method returns a view(). Currently when I am returning the View(), layout page data are not updating.
- "updating data in the layout section" How you load your data? Could you share your code ?Qing Guo– Qing Guo2022-12-20 07:55:20 +00:00Commented Dec 20, 2022 at 7:55
- Loading the data from session. In the action method we updating the session values and returning the view(), But not affecting on the _layout page.Nikhil_TP– Nikhil_TP2022-12-20 08:03:54 +00:00Commented Dec 20, 2022 at 8:03
- Are you sure about what layout page are you trying to update? Do you have another layout page in the discovery path used by the engine?Steve– Steve2022-12-20 08:19:03 +00:00Commented Dec 20, 2022 at 8:19
Add a comment |
1 Answer
Below is a demo to use session to pass the string to appear in _layout page in every page , you can refer to it.
1.Register IHttpContextAccessor and session in Program.cs
builder.Services.AddSession(); builder.Services.AddHttpContextAccessor(); ... app.UseSession(); 2.In the controller, set a session variable:
HttpContext.Session.SetString("Photo", "p1"); 3.In _layout.cshtml, inject IHttpContextAccessor implementation and use it to get the HttpContext and Session object from that.
@using Microsoft.AspNetCore.Http @inject IHttpContextAccessor HttpContextAccessor ... @HttpContextAccessor.HttpContext.Session.GetString("Photo") result:
