【问题标题】:Load a particular JS file base on what URL its hosted根据托管的 URL 加载特定的 JS 文件
【发布时间】:2016-04-11 23:37:16
【问题描述】:

我有一个包含以下代码的 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 或者如果它在 Test 中。我希望能够以某种方式动态地做到这一点。一种解决方案可能是它是否可以读取当前 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 转换。但这比我以前的要好得多。

下一步是在 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>

【问题讨论】:

  • 这看起来不像普通的 HTML。那些@* 是什么东西?你用的是什么框架?
  • 这是来自 ASP MVC 的 Razor
  • 您应该一起设置单独的环境并使用环境变量
  • 你找到答案了吗?
  • 是的,我已经更新了这个问题。感谢您的帮助。

标签: javascript production-environment test-environments


【解决方案1】:

你可以检查这个答案: https://stackoverflow.com/a/5819693/2710681

或者,您可以使用 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." );
    });
}

【讨论】:

    【解决方案2】:

    由于您使用的是 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),如果它在 DEBUG 中构建,则设置为 true ,使用preprocessor directives

    [代码]

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

    【讨论】:

      【解决方案3】:

      你的 if 语句可能是

      var a = document.createElement('a');
      a.href = url;
      var hostname = a.hostname;
      
      if(hostname == "yourlivewebsite.com") {
      
      } else {
      }
      

      【讨论】:

      • 这太骇人听闻了。
      【解决方案4】:

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

      More info on web config transformations

      然后,我将使用 web 配置参数来确定适当的环境,该环境通过 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>
      

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

      【讨论】:

      • 嗨whipdancer,如果@jsUrl 在cshtml 页面上的当前上下文中不存在,上述操作将如何工作?
      • 如果@jsUrl 不存在,它将无法工作。使其成为 web.config 驱动的要点 - 这样您正在使用的配置(调试、测试、发布等)将在 web.config 中提供正确的值,当应用程序启动时它将填充 jsUrl 和您将在视图中看到它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2013-03-05
      • 2014-03-18
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多