MVC 3 Razor _ViewStart中的公共变量 [英] Public variables in MVC 3 Razor _ViewStart

查看:111
本文介绍了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.

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