Skimrande
Skimrande

Reputation: 831

Sharing variable across Razor views in ASP .NET Core 3.1

I have a ASP .NET Core 3.1 MVC application. In one of my razor Views I have a variable which points to a session value.

@{
    var userid = Context.Session.GetString("userid");
}

I was wondering how I could make it available to all views, without having to declare it in each of them. I read this question which asked more or less the same thing, however asked 9 years ago. Public variables in MVC 3 Razor _ViewStart

Can _ViewImports or _ViewStarts be used for this purpose, if so, how?

Thanks in advance!

Upvotes: 1

Views: 1799

Answers (1)

Skimrande
Skimrande

Reputation: 831

"Instead of creating variables, you're better off injecting a service that will generate the values you need as required."

as written by DavidG @ https://stackoverflow.com/a/47858367/2768479

Upvotes: 1

Related Questions