Nikhil_TP
Nikhil_TP

Reputation: 81

In ASP.net core, How to Reload the _layout page when retun view()

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.

Upvotes: 3

Views: 869

Answers (1)

Qing Guo
Qing Guo

Reputation: 9112

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:

enter image description here

Upvotes: 3

Related Questions