0

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

4 回答 4

2

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

有关 Web 配置转换的更多信息

然后,我将使用 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>

** 我认为这段代码不会按原样运行。这是给你一个大致的想法。

于 2016-01-07T16:46:34.230 回答
0

你可以检查这个答案: 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." );
    });
}
于 2016-01-07T14:34:29.657 回答
0

由于您使用的是 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>
}

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

编辑:如果您没有环境变量,则可以将 abool object从控制器传递到视图(使用ViewBag),如果它是在 DEBUG 中构建的,则使用preprocessor directives设置为 true 。

[代码]

bool isLive = true;
#if DEBUG
isLive = false;
#end if;
于 2016-01-07T14:39:44.427 回答
-1

你的 if 语句可能是

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

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

} else {
}
于 2016-01-07T14:42:05.323 回答