根据 URL 托管的内容加载特定的 JS 文件

Load a particular JS file base on what URL its hosted

我有一个包含以下代码的 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>*@

基本上,我评论或取消评论我想在网站上线或测试中使用的脚本。我希望能够以某种方式动态地做到这一点。也许,一种解决方案是它是否可以读取当前的 URL 并弄清楚它是否是 Live/Test.

更新了答案(感谢 Whipdancer)

在 Web.config 我添加了 url:

<add key="BundleJS" value="https://TEST_DOMAIN.com/test.js" />
<!--<add key="BundleJS" value="https://LIVE_DOMAIN.com/Live.js" />

接下来我将研究 web.config 转换。但这比我以前的好多了。

下一步是在 Session_Start 期间,在 Global.asax.cs 文件中,我将 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>

你可以查看这个答案:

或者,您可以使用 jQuery 的 Ajax getScript 方法进行测试,但上面的方法可能更好:

if (test) {
    $.getScript( "https://TEST_DOMAIN.com/test.js", function( data, textStatus, jqxhr ) {
      console.log( data ); // Data returned
      console.log( textStatus ); // Success
      console.log( jqxhr.status ); // 200
      console.log( "Test load was performed." );
    });
} else {
    $.getScript( "https://LIVE_DOMAIN.com/Live.js", function( data, textStatus, jqxhr ) {
      console.log( data ); // Data returned
      console.log( textStatus ); // Success
      console.log( jqxhr.status ); // 200
      console.log( "Live load was performed." );
    });
}

由于您使用的是 Razor,因此您可以在服务器端执行此操作:

@if(isLive)
{
    <script type="text/javascript" src="https://TEST_DOMAIN.com/test.js"> </script>
}
else
{
    <script type="text/javascript" src="https://LIVE_DOMAIN.com/Live.js"> </script>
}

其中 isLive 是一个变量,指示当前环境是测试还是实时。此解决方案将 运行 服务器端并且不会用更多脚本

污染 HTML

编辑: 如果您没有环境变量,您可以将 bool object 从控制器传递到视图(使用 ViewBag), setting to true if it's build in DEBUG, using preprocessor directives

[代码]

bool isLive = true;
#if DEBUG
isLive = false;
#end if;

你的 if 语句可以是

var a = document.createElement('a');
a.href = url;
var hostname = a.hostname;

if(hostname == "yourlivewebsite.com") {

} else {
}

由于您使用的是 ASP.NET MVC - 您可以使用 Web 配置转换来处理不同的环境。

More info on web config transformations

然后我会使用网络配置参数来确定适当的环境,该环境通过 global.asax(或者可能在我的主视图控制器中)加载。

然后您将能够根据您编译到的环境自动确定合适的URL。

in test web.config:

<appSettings>
    <add key="JSSource" value="https://TEST_DOMAIN.com/test.js" />
</appSettings>

in release web.config:

<appSettings>
    <add key="JSSource" value="https://LIVE_DOMAIN.com/Live.js" />
</appSettings>

在 global.asax 中你可以这样做:

public class MvcApplication : System.Web.HttpApplication
{
    public static string jsUrl;

    protected void Application_Start()
    {
        jsUrl = System.Configuration.ConfigurationManager.AppSettings["JSSource"];
    }
}

在你的页面中你可以使用这样的东西:

<script type="text/javascript" src="@jsUrl"> </script>

** 我认为这段代码不会 运行 原样。就是给大家一个大概的思路。