我目前正在我的主模板上设置部分视图,如下所示:
@inherits UmbracoTemplatePage
@{
Layout = null;
IPublishedContent global = Umbraco.TypedContentSingleAtXPath("//*[@nodeTypeAlias='Global']");
}
<!DOCTYPE html>
<html>
<body>
@Html.Partial("Header", global)
@RenderBody()
@Html.Partial("Footer", global)
</body>
</html>
被调用的全局节点包含页眉和页脚工作所需的所有内容,即内容管理的徽标和导航。事实上,这很好。
但是,每次我想要获取全局设置时,必须声明并传递此全局节点会感到浪费。我的背景是使用WebForms,所以虽然我可以创建一个类并且每次都调用它来获取我想知道MVC类型处理它的节点。
有没有人知道我可以将此声明合并到模型或控制器中的简洁方法,以便我可以在需要时调用全局设置,而不必先声明并调用我的部分视图?
答案 0 :(得分:2)
我只想使用在bootstrapping / global.asax期间初始化的singleton / statoc对象。
然后你应该可以从任何你想要的地方访问它......
:编辑: 举个例子:
创建一个提供静态属性的类
public class Global
{
public static string SomeProperty { get; private set; }
internal static void Initialize()
{
// do initialize your global properties
SomeProperty = "Something";
}
}
在全局asax
期间初始化它protected void Application_Start()
{
Global.Initialize();
}
在您的观看中使用它:
@Global.SomeProperty