【问题标题】:Error using a variable declared in _AppStart VB使用在 _AppStart VB 中声明的变量时出错
【发布时间】:2013-07-28 22:58:48
【问题描述】:

我正在使用 Visual Studio 2012 中的 ASP 网页。我开始进行一些测试,其中一个测试 ois 在网站项目的文件中使用全局/站点范围的变量。 问题是在项目的文件中使用声明的变量时我不能并得到一个错误。可能我错过了什么或者我犯了一个错误。

我的代码如下 文件:_AppStart.vbhtml(在我网站的根目录中)

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

结束代码

文件:App_Layout/defaultTheme/_header.vbhtml

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

如果我使用@sitename,我会收到错误:'sitename' is not declared。由于其保护级别,它可能无法访问。如果我使用文本项目运行完美。

我正在学习http://www.w3schools.com/aspnet/webpages_global.asp中的基础教程

任何帮助都会很好。提前致谢。

【问题讨论】:

    标签: asp.net asp.net-mvc vb.net razor


    【解决方案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>
    

    【讨论】:

    • 效果很好,感谢您的帮助,我按照建议更换了,现在效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多