如何将数据从StartPage(_ViewStart.cshtml)传递到Layout.cshtml?

时间:2012-10-24 22:43:46

标签: c# asp.net-mvc asp.net-mvc-4

在我的布局页面中,我有一个横幅,我只想在Staging环境中显示。一旦部署到Production,我怎样才能自动隐藏它?

_ViewStart.cshtml中,我可以使用System.Environment.GetEnvironmentVariable("web_env")获取所需信息,但如何隐藏布局页面中的div?我无法在控制器中返回ActionResult,我可以吗?

<div id='warning'>blah</div>

更新

_ViewStart.cshtml

PageData["IsProd"] = System.Environment.GetEnvironmentVariable("web_env") == "PROD";

Layout.cshtml

@if (!PageData["IsProd"])
{
    <div id="warning">
        you are in testing environment.
    </div>
}

2 个答案:

答案 0 :(得分:1)

在您的视图中:

@if(isNotProductionEnvironment)
{
    <div id='warning'>This will be visible anywhere but production</div>
}

答案 1 :(得分:0)

您可以在web.config

中使用条件语句并在AppSettings中设置一个值
@if(System.Web.Configuration.WebConfigurationManager.AppSettings["IsDev"] == "1")
{
   <div id='warning'>blah</div>
}

@if(System.Environment.GetEnvironmentVariable("web_env") == "0")
{
   <div id='warning'>blah</div>
}