如何配置asp.net mvc站点以包含特定发布模式的脚本

时间:2021-07-12 05:55:03

We have setup three environments DEV, UAT and Live for our website built in Asp.net MVC. We need to add the google analytics script code (as below) on the live website only. How can we possibly configure this or any script so that when we publish the site in the release mode or another mode only then this script is added before the ending body tag of the _Layout view? thanks

我们为Asp.net MVC内置的网站设置了三个环境DEV,UAT和Live。我们只需要在实时网站上添加Google Analytics分析脚本代码(如下所示)。我们怎么可能配置这个或任何脚本,以便当我们在发布模式或其他模式下发布网站时,这个脚本会添加到_Layout视图的结束正文标记之前?谢谢

<script>

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxxxx-1', 'auto');
ga('send', 'pageview');

</script>

2 个解决方案

#1


Put the script in it's own file GoogleAnalytics.js.

将脚本放在自己的文件GoogleAnalytics.js中。

Add it to a bundle with the #if Preprocessor Directive

使用#if预处理器指令将其添加到捆绑包中

  bundles.Add(new ScriptBundle("~/bundles/ga").Include(
#if(!DEBUG)
    "~/Scripts/GoogleAnalytics.js"),
#endif
   );

When you render the bundle, if it has no scripts nothing is rendered and no error messages.

渲染包时,如果没有脚本,则不会呈现任何内容,也不会显示任何错误消息。

#2


You can use conditional compilation symbols. They are specific for build types. Right-click on your web project, go to properties, then go to build tab. There will be "Conditional compilation symbols" tab. Select the build type that should have analytics (Live). Add something like USER_GOOGLE_ANALYTICS there.

您可以使用条件编译符号。它们特定于构建类型。右键单击您的Web项目,转到属性,然后转到构建选项卡。将有“条件编译符号”选项卡。选择应具有分析(实时)的构建类型。在那里添加类似USER_GOOGLE_ANALYTICS的内容。

Now you can add "custom" analytics code via ifdef:

现在,您可以通过ifdef添加“自定义”分析代码:

#if USER_GOOGLE_ANALYTICS
//code
#endif

This will not be in your view, but the code can modify something that changes your view rendering.

这不会在您的视图中,但代码可以修改更改视图呈现的内容。

#1


Put the script in it's own file GoogleAnalytics.js.

将脚本放在自己的文件GoogleAnalytics.js中。

Add it to a bundle with the #if Preprocessor Directive

使用#if预处理器指令将其添加到捆绑包中

  bundles.Add(new ScriptBundle("~/bundles/ga").Include(
#if(!DEBUG)
    "~/Scripts/GoogleAnalytics.js"),
#endif
   );

When you render the bundle, if it has no scripts nothing is rendered and no error messages.

渲染包时,如果没有脚本,则不会呈现任何内容,也不会显示任何错误消息。

#2


You can use conditional compilation symbols. They are specific for build types. Right-click on your web project, go to properties, then go to build tab. There will be "Conditional compilation symbols" tab. Select the build type that should have analytics (Live). Add something like USER_GOOGLE_ANALYTICS there.

您可以使用条件编译符号。它们特定于构建类型。右键单击您的Web项目,转到属性,然后转到构建选项卡。将有“条件编译符号”选项卡。选择应具有分析(实时)的构建类型。在那里添加类似USER_GOOGLE_ANALYTICS的内容。

Now you can add "custom" analytics code via ifdef:

现在,您可以通过ifdef添加“自定义”分析代码:

#if USER_GOOGLE_ANALYTICS
//code
#endif

This will not be in your view, but the code can modify something that changes your view rendering.

这不会在您的视图中,但代码可以修改更改视图呈现的内容。