c# - 自动将查询字符串从页面请求附加到 ASp.NET WebForms 中的出站页面请求

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

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

谢谢

最佳答案

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

将以下规则添加到您的 web.config 将导致请求 URL 中的所有查询字符串值附加到响应中每个 anchor 标记的 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>

关于c# - 自动将查询字符串从页面请求附加到 ASp.NET WebForms 中的出站页面请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7690882/

相关文章:

asp.net - 是什么让某些东西成为 ASP.NET Core 中的请求功能?

c# - 将鼠标悬停在 ASP.NET 按钮上时显示注释

asp.net - 在 WebPart 编辑器区域中添加自定义属性

html - 我应该将所有链接设为相对链接还是绝对链接

c# - 如何使用我自己的方法或为 EF Core 查询编写 DbFunction (EF Core 3.0)

c# - 为什么对 Azure Document DB 的第一个请求比后续请求慢得多?

c# - Visual Studio 将 bin 文件添加到项目

c# - 将字符串附加到 URL

javascript - 使用 javascript 检查 URL 是否包含值

c# - WebClient.DownloadStringAsync 在 Silverlight 中抛出安全异常