C# 在ASP.NET核心MVC中重写URL赢得';不能正确使用反向引用

C# 在ASP.NET核心MVC中重写URL赢得';不能正确使用反向引用,c#,asp.net-mvc,url-rewriting,asp.net-core-mvc,C#,Asp.net Mvc,Url Rewriting,Asp.net Core Mvc,这是本书的延续。我在.NET Core 3.1中有一个ASP.NET MVC网站,我正在尝试重新编写URL以将查询字符串值放入路径中,例如/api/values?id=1。我从下载了示例应用程序,并将自己的规则添加到iisurelrewrite.xml文件中,如下所示: <rewrite> <rules> <rule name="my rule" stopProcessing="true">

这是本书的延续。我在.NET Core 3.1中有一个ASP.NET MVC网站,我正在尝试重新编写URL以将查询字符串值放入路径中,例如
/api/values?id=1
。我从下载了示例应用程序,并将自己的规则添加到
iisurelrewrite.xml
文件中,如下所示:

<rewrite>
  <rules>
      <rule name="my rule" stopProcessing="true">
          <match url="^api/(\w+)$" />
          <condition>
              <add input="{QUERY_STRING}" pattern="id=([0-9]+)" />
          </condition>
          <action type="Rewrite" url="api/{R:1}/{C:1}" appendQueryString="false"/>
      </rule>
  </rules>
</rewrite>
这样做使得重写的URL是
api/Values/1?id=1
。因此,即使
appendQueryString
属性设置为
false
,整个查询字符串仍在追加。


我错过了什么?谢谢。

根据源代码,这似乎是内置功能,asp.net core appendquerystring属性与IIS的属性不同,它将附加新的querystring而不是删除它

问题是这样的:

      <rule name="some other rule" stopProcessing="true">
          <match url="^api/(\w+)$" />
          <condition>
              <add input="{QUERY_STRING}" pattern="id=1" />
          </condition>
          <action type="Rewrite" url="api/{R:1}/1" appendQueryString="false"/>
      </rule>
        if (pattern.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal) >= 0)
        {
            string scheme;
            HostString host;
            PathString path;
            QueryString query;
            FragmentString fragment;
            UriHelper.FromAbsolute(pattern, out scheme, out host, out path, out query, out fragment);

            if (query.HasValue)
            {
                if (QueryStringAppend)
                {
                    request.QueryString = request.QueryString.Add(query);
                }
                else
                {
                    request.QueryString = query;
                }
            }
            else if (QueryStringDelete)
            {
                request.QueryString = QueryString.Empty;
            }

            request.Scheme = scheme;
            request.Host = host;
            request.Path = path;
        }

谢谢你。事实证明,这是一个已知的问题: