MVC 3 Razor _ViewStart中的公共变量 [英] Public variables in MVC 3 Razor _ViewStart
问题描述
我正在MVC 3附带的新Razor引擎上构建一个站点(并且喜欢新语法!).但是,我对将其与公共属性/常量一起使用一无所知.我知道,使用WebForms,我们可以在代码后面添加一个公共属性:
I'm building a site on the new Razor engine that comes with MVC 3 (and loving the new syntax!). However, I am at a loss about using public properties / constants with it. I know that with WebForms we could add a public property in code behind:
public string ImageFolder { get; set; }
我想在一个全局视图中定义重要的变量,我的视图可以从CSS文件和图像的路径开始:
I would like to define important variables in one global place that my views can access, starting with paths to CSS files and images:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
var ContentFolder = "~/Content";
var CssFolder = ContentFolder + "/Stylesheets";
var ImageFolder = ContentFolder + "/Images";
}
我尝试将上面的代码块放入_Layout
以及_ViewStart
内.但是,从子视图访问它们失败了.我想在上面的代码块中定义一个公共属性,但是它不能编译.
I have tried putting the above code block in _Layout
, as well as inside _ViewStart
. However, accessing them from child views fails miserably. I thought of defining a public property in the above code block but it doesn't compile.