WebMatrix中2:存储静态值 [英] Webmatrix 2: Storing static values

查看:164
本文介绍了WebMatrix中2:存储静态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将在其中储存静态值的最佳场所。
我将如何访问它。

我希望能够从任何页面访问静态值。但是,只有在一个地方来定义它们。

例如电子邮件和phoneNumber的

For example 'email' and 'phoneNumber'

我已经试过的东西像会话 PageData ,并确定在我的头中的变量(所有页面使用),但这是行不通的。

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.

例如。第一次加载:

页面载入< - 试图访问变量。不会被初始化。

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

报头部分加载&LT; - 可变initalised

Header Partial Loaded <- Variable initalised.

结果。页面不不是显示变量。

Result. Page does not show variable.

我曾考虑将其存储在配置文件中。但我不知道如何从WebMatrix的2访问此。
我可以建立一个TXT / ini文件或东西,但肯定解析文件没有做到这一点的最好办法。 - 因为我已经尝试这样做,它似乎像MVC3(配置)有效,txt文件是不实际的阅读为每个请求

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.

推荐答案

由静态的,如果你的意思是不要在整个应用程序的生命周期变化值,​​你通常会使用的存储HelperPage.App财产。它是基于动态键入这样你就可以创建任意属性:

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"; 

或者你可以使用与传统的名称/值征收方式<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.webpages.webpageexecutingbase.appstate%28v=vs.111%29\"相对=nofollow>届时AppState :

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

不过,如果你的静态的变量是特定于用户,你应该使用的Session的范围设置为用户。或者,如果你想永久地储存它们使用一个数据库。

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.

您可以设置_PageStart.cshtml(可能需要创建)会话值,或在Global.asax文件在session_start事件,然后访问它们中的任何部分/查看你的愿望。

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.

这篇关于WebMatrix中2:存储静态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆