开发者

Automatically append Query String from page request to outbound page request in ASp.NET WebForms

开发者 https://www.devze.com 2023-04-11 18:06 出处:网络
I maintain a moderate sized public internet website for a company built using C# and ASP.Net WebForms using the .NET Framework 3.5.One of our 3rd-party marketing/lead generation consultants wants to r

I maintain a moderate sized public internet website for a company built using C# and ASP.Net WebForms using the .NET Framework 3.5. One of our 3rd-party marketing/lead generation consultants wants to reserve several query string codes that go into Google Analytics for themselves and have those codes persist as the user moves around the website. Since (for obvious reasons) I don't really want to have to touch every place we generate an internal开发者_高级运维 URL link to maintain these codes, is there anywhere in the ASP.NET page lifecycle where I could intercept these (either when they are being written when the page is served or after the link is clicked) so I do not have to change the links generated on dozens of pages and hundreds of individual instances.

Thanks


If you're on IIS 7, look into outbound rules using the IIS URL Rewrite module - http://learn.iis.net/page.aspx/657/creating-outbound-rules-for-url-rewrite-module/. They're extremely powerful. You may be able to do this with older versions of IIS as well, but I've only worked with 7.

Adding the following rules to your web.config will cause all query string values in the request URL to be appended to the URLs in the hrefs of every anchor tag in the response. The rules will apply to every page in your site.

<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>


You probably want to look at HTTP Modules. There is a little bit of information in this KB article: http://support.microsoft.com/kb/307985. You could catch the BeginRequest and possible modify the request or redirect the request.

0

精彩评论

暂无评论...
验证码 换一张
取 消