4

我正在尝试制定一个将 URL 参数附加到 URL 的 IIS URL 重写规则。url 参数是hssc. 因此,通过服务器处理的任何 url 都需要该参数。请记住,某些 url 已经有自己的参数,而其他 url 不会,以及根 url 等,有时它需要添加?hssc=1&hssc=- 所以,如果我有一个这样的 URL:

我还希望 URL 不应该被隐藏(就像在后台重写一样)。我需要 URL 出现在 URL 中,所以当用户复制 URL 或添加书签时,参数就在那里。

我已经设置了条件来匹配它\&hssc|\?hssc——现在我只需要一种方法来编写 URL,这样它就会出现并保留已经存在的原始 URL 的一部分。

4

2 回答 2

5

想通了 - 规则将这样设置:

<rewrite>
            <rules>
                <rule name="sigh" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="(.*)" negate="false" />
                    <action type="Redirect" url="{PATH_INFO}?hssc=1" />
                    <conditions>
                        <add input="{QUERY_STRING}" pattern="^hssc=|\?hssc=|\&amp;hssc=" negate="true" />
                    </conditions>
                </rule>
            </rules>
        </rewrite>
于 2012-09-30T06:26:40.677 回答
0

 

只是作为通过代码维护 QS 的提示:

public static string GetQS()
{
  var sb = new System.Text.StringBuilder();

  System.Collections.Specialized.NameValueCollection Qs = HttpContext.Current.Request.QueryString;

  var lst = new List<string>(Qs.AllKeys);

  foreach (string s in lst)
  {
    if (s == "MyOwnQSKey")
      sb.Append(s + "=" + "MyValue" + "&");
    else
      sb.Append(s + "=" + Qs[s] + "&");
  }

  if (!lst.Contains("MyOwnQSKey"))
    sb.Append("MyOwnQSKey=MyValue&");

  if (sb.ToString().EndsWith("&"))
    sb.Remove(sb.Length - 1, 1);

  return "?" + sb;
}

我刚刚从我的一个项目中复制了这个,类似于你在 Url-Rewrite 和 QS 中的项目

希望这有帮助

 

于 2012-09-28T23:25:15.473 回答