【问题标题】:How to Inject HTML on ASP.NET MVC master page如何在 ASP.NET MVC 母版页上注入 HTML
【发布时间】:2014-12-01 23:54:07
【问题描述】:

关于如何在 ASP.NET MVC 母版页中注入 HTML,我有一个简单但可能很常见的问题。我的母版页上有一个谷歌分析跟踪代码。代码如下所示:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_trackPageview']);
// need to inject ecommerce code here
(function () {
    // google analytics code here
})();

我正在使用电子商务跟踪,我想在收据页面上的这个 HTML 中注入“购物车”信息(并且只在这个页面上)。所以我做了这样的事情:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_trackPageview']);
<% if(ViewData["googleanalytics"]!=null) {%>
<%= ViewData["googleanalytics"] %>
<% } %>
(function () {
    // google analytics code here
})();

然后在控制器中,我的代码如下所示:

[HttpGet]
public ActionResult Receipt()
{
    var receipt = // get receipt model

    // get google analytics javascript. This function pulls
    // the data from the receipt model
    ViewData["googleanalytics"] = GetAnalyticsInfo(receipt); 

    return View(receipt);
}

这整个事情看起来有点杂乱无章,我想知道是否有人有更好的想法来处理这种情况?

【问题讨论】:

  • 您到底想做什么?哪一部分最让你烦恼?据我了解,您的问题是整个网站的 Google Analytics(分析)代码是不变的,但电子商务代码是每页还是其他?
  • 您可以编写一个过滤器来设置 ViewData 中的 GA 代码并将其放在 Receipt 操作中。 (我认为这是适合使用 ViewData 的罕见场景之一。)不过,将其留在操作中确实没有任何问题。
  • 是的,电子商务只针对收据页面。

标签: asp.net asp.net-mvc master-pages google-analytics


【解决方案1】:

我想我误读了你原来的问题。如果这只发生在一个页面上,那么添加过滤器将是一个“更清洁”的选项。

过滤器:

public sealed class GAFilter: ActionFilterAttribute
{
   public override void OnActionExecuting(ActionExecutingContext filterContext)
   {
       filterContext.Controller.ViewData["googleanalytics"] = GetAnalyticsInfo();
       base.OnActionExecuting(filterContext);
   }

   private MvcHtmlString GetAnalyticsInfo()
   {
   }
}

行动:

[GA]
public ActionResult ()
{
    return View();
}

如果您可以将强类型视图模型绑定到 MasterView,那就太好了,但当前的 MVC 框架不允许这样做。 ViewData 是您将信息从 Controller 推送到 Master 的朋友。

【讨论】:

  • 由于 GetAnalyticsInfo() 需要访问模型,我将如何获取这些信息?
  • 当你说Model时,你指的是ViewModel还是DataModel?我只会将此信息保留在 web.config 的应用程序设置中或从 Config DB 中获取。
  • 任一。为了论证的缘故,让我们说 ViewModel。关键是 GetAnalyticsInfo() 必须返回用户特定的收据信息。这是在控制器中生成的。
  • 据我了解,MasterView 没有 ViewModel,因为 View 只能有一个 ViewModel。您可以通过 ActionExecutingContext 访问控制器。更新:请参阅下面我的答案中的代码。
【解决方案2】:

你能把javascript分成两部分吗:

在母版页的顶部,输入:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_trackPageview']);

在您的母版页底部,输入:

(function () {
    // google analytics code here
})();

然后,在收据页面视图的某个位置(它将在母版页的两段 javascript 之间输出),放入

<% if(ViewData["googleanalytics"]!=null) {%>
<%= ViewData["googleanalytics"] %>
<% } %>

值得一试吗?

【讨论】:

    【解决方案3】:

    我觉得您希望在所有页面上全面应用 GA 代码?在这种情况下,您可以将帐户 ID 抽象为一个帮助程序类,如下所示:

    public class Configuration {

    公共字符串 GoogleAnalyticsId { 得到 { 返回“INSERT_ID_HERE”; } }

    }

    然后您可以在局部视图中引用它。将整个 JS 推送到一个 .ascx 文件中(在 Views\Shared 下)

    var _gaq = _gaq || []; _gaq.push(['_setAccount', '<%=Configuration.GoogleAnalyticsId%>']); _gaq.push(['_trackPageview']); // need to inject ecommerce code here (function () { // google analytics code here })();

    现在,向你的 master 添加一个 Html.Partial

    <%=Html.Partial("GoogleAnalyticsControl")%>

    【讨论】:

    • 问题是您仍然没有提取特定收据的谷歌分析代码。
    【解决方案4】:

    public override void OnActionExecuting(ActionExecutingContext filterContext) { MyController myController = (MyController)filterContext.Controller; myController.GetAnalyticsInfo(); base.OnActionExecuting(filterContext); }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      相关资源
      最近更新 更多