Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如果URL=xxx,则在cshtml mvc中显示文本_Asp.net_Asp.net Mvc_Razor_Model View Controller - Fatal编程技术网

Asp.net 如果URL=xxx,则在cshtml mvc中显示文本

Asp.net 如果URL=xxx,则在cshtml mvc中显示文本,asp.net,asp.net-mvc,razor,model-view-controller,Asp.net,Asp.net Mvc,Razor,Model View Controller,我有两个具有cshtml视图的mvc应用程序,它们都具有不同的url。它们是验收和生产应用程序。我需要根据url更改/隐藏homeview上的一些文本。例如:如果URL包含单词“production”,我需要将文本“production”添加到主屏幕上的div中 有人能给我指出正确的方向吗?使用ViewBags。在控制器中声明ViewBag,例如: ViewBag.Type = "Production" 在生产页面上,以及: ViewBag.Type = "Other Type" 在控制器的

我有两个具有cshtml视图的mvc应用程序,它们都具有不同的url。它们是验收和生产应用程序。我需要根据url更改/隐藏homeview上的一些文本。例如:如果URL包含单词“production”,我需要将文本“production”添加到主屏幕上的div中


有人能给我指出正确的方向吗?

使用ViewBags。在控制器中声明ViewBag,例如:

ViewBag.Type = "Production"
在生产页面上,以及:

ViewBag.Type = "Other Type"
在控制器的其他方法中。然后在homeview中检查此ViewBag下的内容

if (!string.IsNullOrEmpty(ViewBag.Type))
{
    if (ViewBag.Type == "Production")
    {
        <p>Production</p>
    }
    else if (ViewBag.Type == "Other Type")
    {
        <p>Other Type</p>
    }
}
if(!string.IsNullOrEmpty(ViewBag.Type))
{
如果(ViewBag.Type==“生产”)
{
生产

} else if(ViewBag.Type==“其他类型”) { 其他类型

} }
使用可视包。在控制器中声明ViewBag,例如:

ViewBag.Type = "Production"
在生产页面上,以及:

ViewBag.Type = "Other Type"
在控制器的其他方法中。然后在homeview中检查此ViewBag下的内容

if (!string.IsNullOrEmpty(ViewBag.Type))
{
    if (ViewBag.Type == "Production")
    {
        <p>Production</p>
    }
    else if (ViewBag.Type == "Other Type")
    {
        <p>Other Type</p>
    }
}
if(!string.IsNullOrEmpty(ViewBag.Type))
{
如果(ViewBag.Type==“生产”)
{
生产

} else if(ViewBag.Type==“其他类型”) { 其他类型

} }
有更好的方法

您可以使用web.config转换,这样您将有不同的配置文件用于测试和生产

在web.config中创建appsetting

<appSettings>
            <add key="Environment" value="Test" />
</appSettings>

有更好的方法可以做到这一点

您可以使用web.config转换,这样您将有不同的配置文件用于测试和生产

在web.config中创建appsetting

<appSettings>
            <add key="Environment" value="Test" />
</appSettings>

您可以将此answers用作div元素。如果您唯一的选择是从URL读取,则在控制器操作中读取Request.Uri,并将环境名称传递给视图。理想情况下,您应该为这些事情设置一个配置。您可以将此answers用作div元素。如果您唯一的选择是从URL读取,则在控制器操作中读取Request.Uri,并将环境名称传递给视图。理想情况下,您应该为这些事情设置一个配置。