我有一个包含以下代码的 MVC ASP 页面:
<script type="text/javascript" src="https://TEST_DOMAIN.com/test.js"> </script>
@*<script type="text/javascript" src="https://LIVE_DOMAIN.com/Live.js"> </script>*@
基本上我评论或取消评论我想使用的脚本,如果网站是 Live 或者如果它在测试中。我希望能够以某种方式动态地做到这一点。也许一种解决方案是它是否可以读取当前 URL 并确定其是否为 Live/Test。
更新了答案(感谢 Whipdancer)
在 Web.config 我添加了网址:
<add key="BundleJS" value="https://TEST_DOMAIN.com/test.js" />
<!--<add key="BundleJS" value="https://LIVE_DOMAIN.com/Live.js" />
接下来我将研究 web.config 转换。但这比我以前的要好得多。
下一步是在 Global.asax.cs 文件中的 Session_Start 期间,我将 url 设置为应用程序变量:
Application["BundleJS"] = System.Configuration.ConfigurationManager.AppSettings["BundleJS"];
设置好之后,我可以转到视图的控制器(提示:在我的情况下,视图是一个布局,所以我转到了父控制器)。在 ActionResult 方法上或之后,我将 Application 变量添加到 viewbag
ViewBag.BundleJS = HttpContext.Application["BundleJS"];
最后在cshtml页面(对我来说是_layout)我设置了脚本
<script type="text/javascript" src="@ViewBag.BundleJS"> </script>