You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2022中ASP.NET Core Web应用忽略cshtml页面<environment include="Development">配置的问题求助

Visual Studio 2022中ASP.NET Core Web应用忽略cshtml页面配置的问题求助

各位大佬好,我现在碰到一个特别头疼的问题:在Visual Studio 2022的ASP.NET Core Web项目里,cshtml页面的<environment>标签完全不起作用了——不管是include="Development"还是exclude="Development"的区块都会被渲染出来,直接导致Bootstrap重复加载报错。我已经做了一堆排查,但还是没找到问题根源,想请大家帮忙看看!

我已经做过的排查步骤:

  • 确认launchsettings.json配置:浏览器地址栏的端口和配置里的一致,所有profile的ASPNETCORE_ENVIRONMENT都明确设为Development
  • 在Program.cs里加了断点和日志验证,app.Environment.IsDevelopment()确实返回true,控制台也输出了"Development"
  • 在cshtml里写了多组<environment>测试区块,结果所有区块的内容都原封不动显示在页面上

相关配置与代码

launchsettings.json 内容

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56024",
      "sslPort": 44391
    }
  },
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5155",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7237",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Program.cs 中的环境验证代码

if (!app.Environment.IsDevelopment())
{
    Console.WriteLine("Not Development");
}
else
{
    Console.WriteLine("Development"); // <- 这个断点确实命中了
}

Index.cshtml 中的测试代码

@* For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 *@
@{
    var mytitle = ViewBag.Title;
}
<h1 text-align: center;>@mytitle</h1>

<environment include="Development">
    <div>Environment is Development</div>
</environment>
<environment exclude="Development">
    <div>Environment is NOT Development</div>
</environment>
<environment include="Staging,Development,Staging_2">
    <div>Environment is: Staging, Development or Staging_2</div>
</environment>

<button type="submit" class="btn btn-primary">Login</button>

现在的问题是:明明环境确实是Development,但浏览器里三个<environment>区块的内容全部都显示出来了,exclude的区块完全没被过滤。我还截了Edge浏览器显示页面的图,能看到三个div的内容同时存在。

有没有大佬遇到过类似的问题?或者能帮我指出我漏掉了什么配置吗?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 10:09:28