0

I'm working with ASP Web Pages, in Visual Studio 2012. Im starting to make some test and one of those test ois to have global/sitewide variables used in files in the website project. The problem is that i cant and get an error when using the declared variable in a file on the project. Probably im missing something or i'm making a mistake.

The Code i have is the following File: _AppStart.vbhtml (is in the root of my website)

@Code
'Declaring Global Variables for the Application
Dim sitename As String  = "Test Razor App"

End Code

File: App_Layout/defaultTheme/_header.vbhtml

<meta charset="utf-8" />
'<title>@sitename</title>
'<link rel="stylesheet" type="text/css" href="style.css" />

If i use the @sitename i get the error: 'sitename' is not declared. It may be inaccessible due to its protection level. If i use a text the project runs perfectly.

Im following the basic tutorial in http://www.w3schools.com/aspnet/webpages_global.asp

Any Help will be Good. Thanks in Advance.

4

1 回答 1

1

基于此处找到的教程:http ://www.asp.net/web-pages/tutorials/working-with-pages/18-customizing-site-wide-behavior

要设置全局变量,您可以将其存储在 AppState 字典中:

@Code
 'Declaring Global Variables for the Application
 AppState("sitename") = "Test Razor App"
End Code

有关 AppState 的更多信息:http: //msdn.microsoft.com/en-us/library/system.web.webpages.helperpage.appstate (v=vs.111).aspx

然后你会在你的页面中使用它,如下所示:

<title>@AppState("sitename")</title>
于 2013-07-29T10:02:12.127 回答