Webmatrix 2: Storing Static Values

Where would be the best place to store static values. And how can I access it.

I want to have access to static values ​​from any page. But you only need to define them in one place.

For example, "email" and "phoneNumber"

I tried things like Session and PageData , and defined variables in my header (used by all pages), but this does not work.

Partial is initialized after the page, so it either does not work at all, or does not work on the first load.

eg. First time Download:

Loaded <page - tries to access a variable. Not initialized.

Partial loading of the header <- the variable is initialized.

Result. The page shows the variable not .

I considered saving it in a configuration file. But I have no idea how to access this from Webmatrix 2. I could just create a txt / ini file or something, but definitely parsing the file is not the best way to do this. - Since then I tried this, and it does not look correct, as in mvc3 (config), and txt files are inappropriate to read for each request.

+4
source share
1 answer

In the "static" section, if you mean values ​​that do not change throughout the life of the application, you usually use the HelperPage.App property for storage. It is based on the dynamic type, so you can create arbitrary properties:

 App.Email = " someone@somewhere.com "; 

Or you can use a more traditional approach to a set of names / values ​​using AppState :

 AppState["email"] = " someone@somewhere.com "; 

However, if your “static” variables are user-specific, you should use Session as defined by the user. Or use a database if you want to keep them forever.

You can set the session values ​​in _PageStart.cshtml (you may need to create one) or in the Session_Start event of the global.asax file, and then access them in any partial / view you want.

+5
source

Source: https://habr.com/ru/post/1432273/


All Articles