4

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

I want to be able to access static values from any page. But only have to define them in one place.

For example 'email' and 'phoneNumber'

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

The partial is initialised after the page, so it either doesnt work at all, or doesnt work on first time load.

E.g. First time Load:

Page Loaded <- Tries to Access variable. Not initialised.

Header Partial Loaded <- Variable initalised.

Result. Page does not show variable.

I have considered storing it in the config file. But I have no idea how to access this from Webmatrix 2. I could just create a txt/ini file or something but surely parsing a file isn't the best way to do it. - I have since tried this and it doesnt seem valid like in mvc3 (config),and txt files are not practical to read for each request.

Community
  • 1
  • 1
IAmGroot
  • 13,760
  • 18
  • 84
  • 154

1 Answers1

5

By "static", if you mean values that don't change across the lifetime of the application, you would normally use 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 the more traditional name/value collection approach with AppState:

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

However, if your "static" variables are user-specific, you should use Session as that is scoped to the user. Or use a database if you want to store them permanently.

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

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • How would this differ to session – IAmGroot Sep 08 '12 at 04:52
  • Session variables are scoped to a user. AppState variables are scoped to the application. See the amendment I have made to my answer – Mike Brind Sep 08 '12 at 09:36
  • And where would I place this so that I can access it from every page. Note that partials initialise AFTER the page, hence the `session` problem of not working on first time load up. – IAmGroot Sep 09 '12 at 17:04
  • 2
    You can set session values in _PageStart.cshtml, or in the Session_Start event of the global.asax file – Mike Brind Sep 11 '12 at 07:08
  • If by `_PageStart` you mean something like `_siteLayout` its a partial.. And there is no Global.asax file. – IAmGroot Sep 11 '12 at 12:52
  • No - by _PageStart.cshtml, I mean a file that you call _PageStart.cshtml. It is a special file that is executed with every request. Also, you can add a global.asax file by choosing it from the list of file types when adding a new file. You need to filter to All to see global.asax as an option. – Mike Brind Sep 11 '12 at 15:02
  • Thanks! And for all the clarification :) My side project can now continue. You should maybe consider adding the `_PageStart` to your answer, as Im guessing `PageData` and `Session` also work as long as they are there. That was the missing link. – IAmGroot Sep 11 '12 at 15:41
  • I see you have already amended the answer to include your missing link ;o) – Mike Brind Sep 12 '12 at 12:54