【问题标题】:Automatically append Query String from page request to outbound page request in ASp.NET WebForms自动将查询字符串从页面请求附加到 ASp.NET WebForms 中的出站页面请求
【发布时间】:2011-12-03 04:51:46
【问题描述】:

我为一家使用 .NET Framework 3.5 使用 C# 和 ASP.Net WebForms 构建的公司维护一个中等规模的公共互联网网站。我们的一位第 3 方营销/潜在客户生成顾问希望为自己保留几个查询字符串代码,这些代码将进入 Google Analytics,并让这些代码在用户在网站上移动时保持不变。因为(出于显而易见的原因)我真的不想触及我们生成内部 URL 链接以维护这些代码的每个地方,所以在 ASP.NET 页面生命周期中的任何地方我都可以拦截这些(无论是当它们被在提供页面或单击链接后编写),因此我不必更改在数十个页面和数百个单独实例上生成的链接。

谢谢

【问题讨论】:

  • 如果您的顾问希望您随身携带 utm_source 和 utm_campaign,您可以告诉他们他们不必这样做。如果是为了别的,那么你可以忽略这个。

标签: c# asp.net url google-analytics query-string


【解决方案1】:

您可能想查看 HTTP 模块。这篇知识库文章中有一点信息:http://support.microsoft.com/kb/307985。您可以捕获BeginRequest 并可能修改请求或重定向请求。

【讨论】:

    【解决方案2】:

    如果您使用的是 IIS 7,请使用 IIS URL 重写模块 - http://learn.iis.net/page.aspx/657/creating-outbound-rules-for-url-rewrite-module/ 查看出站规则。他们非常强大。您也可以使用旧版本的 IIS 来执行此操作,但我只使用 7。

    将以下规则添加到您的 web.config 将导致 request URL 中的所有查询字符串值附加到 response 中每个锚标记的 href 中的 URL时间>。这些规则将适用于您网站中的每个页面。

    <rewrite>
      <outboundRules>
        <rule name="Add request query string when there is an existing query string" 
              patternSyntax="ECMAScript" stopProcessing="true">
          <match filterByTags="A" pattern="(.+)(\?)(.+)" />
          <conditions>
            <add input="{QUERY_STRING}" pattern=".+" />
          </conditions>
          <action type="Rewrite" value="{R:0}&amp;{C:0}" />
        </rule>
        <rule name="Add request query string when there is a ? but no query string data" 
              patternSyntax="ECMAScript" stopProcessing="true">
          <match filterByTags="A" pattern="(.+)(\?)$" />
          <conditions>
            <add input="{QUERY_STRING}" pattern=".+" />
          </conditions>
          <action type="Rewrite" value="{R:0}{C:0}" />
        </rule>
        <rule name="Add request query string when there is not an existing query string" 
              patternSyntax="ECMAScript" stopProcessing="true">
          <match filterByTags="A" pattern="(.+)(\?){0}" />
          <conditions>
            <add input="{QUERY_STRING}" pattern=".+" />
          </conditions>
          <action type="Rewrite" value="{R:0}?{C:0}" />
        </rule>
      </outboundRules>
    </rewrite>
    

    【讨论】:

    • 德拉茨。我刚刚将它粘贴到我的 Visual Studio 2013 项目中,但出现 web.config 错误。
    猜你喜欢
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2010-10-07
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    相关资源
    最近更新 更多