ASP.NET Core MVC中的URL重写将无法正确使用反向引用 [英] URL rewriting in ASP.NET Core MVC won't use back referencing correctly
问题描述
这是
我想念什么?谢谢.
根据源代码,这似乎是内置功能,asp.net核心的appendquerystring属性与IIS的属性不同,它将附加IIS的属性.而不是删除新的查询字符串.
源代码是这样的:
如果(pattern.IndexOf(Uri.SchemeDelimiter,StringComparison.Ordinal)> = 0){字符串方案HostString主机;PathString路径;QueryString查询;FragmentString片段;UriHelper.FromAbsolute(模式,输出方案,输出主机,输出路径,输出查询,输出片段);如果(query.HasValue){如果(QueryStringAppend){request.QueryString = request.QueryString.Add(query);}别的{request.QueryString =查询;}}否则,如果(QueryStringDelete){request.QueryString = QueryString.Empty;}request.Scheme = scheme;request.Host =主机;request.Path =路径;}
This is a continuation from Parameter binding to either route or querystring in ASP.NET MVC Core. I have an ASP.NET MVC web site in .NET Core 3.1, and I am trying to URL rewrites to put query string values into the path instead, for example /api/Values?id=1
. I have downloaded the sample application from https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/fundamentals/url-rewriting/samples/ and added my own rule to the IISUrlRewrite.xml
file, like so:
<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>
However, when I run the application and go to /api/Values?id=1
, the rewritten URL is /api/Values/?id=1
. So we can see that the rule was applied, and {R:1}
(the back-reference from the path) was properly applied, but {C:1}
, which should be the ID number, is instead the entire query string value.
So I tried changing the rule to be like this:
<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>
Doing this made it so that the rewritten URL was api/Values/1?id=1
. So it appears that even though the appendQueryString
attribute is set to false
, the entire query string is still being appended.
What am I missing? Thanks.
According to the source codes, it seems this is build-in feature, the asp.net core appendquerystring attribute is not as same as IIS's, it will append the new querystring instead of removing it.
The source codes is like this:
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;
}
这篇关于ASP.NET Core MVC中的URL重写将无法正确使用反向引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!