I have a ConfigurationService class that is injected in "_ViewImports":
@inject IConfigurationService ConfigurationService
And I access it in each View Page like below:
@{
// Configurations
var configuration = await ConfigurationService.GetGeneralConfiguration();
var currencySymbol = configuration.Currency.Symbol;
}
This works, but I have to copy and paste the code in each page. I've tried to write the code in _Layout.cshtml
but it doesn't work. Static vars also don't work because the values are not static - for each request I get the current user configuration and inject into the page. And, for all pages that require the currencySymbol
variable value I need to paste code, and I don't want to have to do this.
My question is: How can I declare variables in, for example, _Layout.cshtml
, or _ViewStart.cshtml
or some other view that can be accessed by all razor view pages, like inherits. These variables must contain ConfigurationService values.