Asp.net 如何处理web.config重写规则中的特殊字符?
当URL包含特殊字符时,我的重写规则出错: 此URL 使用此重写规则:Asp.net 如何处理web.config重写规则中的特殊字符?,asp.net,url-rewriting,web-config,special-characters,Asp.net,Url Rewriting,Web Config,Special Characters,当URL包含特殊字符时,我的重写规则出错: 此URL 使用此重写规则: <rule name="rentals by proptype+state+city+street"> <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Zë-+]+)/([0-9a-zA-Zë-+']+)$" /> <action type="Rewrite" url="se
<rule name="rentals by proptype+state+city+street">
<match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Zë-+]+)/([0-9a-zA-Zë-+']+)$" />
<action type="Rewrite" url="search_new.aspx?proptype={R:1}&state={R:2}&city={R:3}&street={R:4}" />
</rule>
<rule name="rentals by proptype+state+city+street">
<match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z-+]+)/([0-9a-zA-Z-+']+)$" />
<action type="Rewrite" url="search_new.aspx?proptype={R:1}&state={R:2}&city={R:3}&street={R:4}" />
</rule>
因此,我想真正的问题是,如何在重写规则中处理
%
字符?您上次的尝试几乎纠正了正则表达式,您只是犯了一个小错误(忘记将0-9添加到第三个块)。正确的regexp是:
^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z0-9%-+]+)/([0-9a-zA-Z%-+'))$
但在重写规则中,您需要使用变量{UNENCODED\u URL}
例如:
<rule name="rentals by proptype+state+city+street" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z0-9%-+]+)/([0-9a-zA-Z%-+']+)$" />
</conditions>
<action type="Rewrite" url="search_new.aspx?proptype={C:1}&state={C:2}&city={C:3}&street={C:4}" />
</rule>
et/savanah/%27s gr交流电eland有一些隐藏的特殊字符(即使如此也无法正确解析)。您可以在此处检查其编码方式:
基于此,我使用以下内容更改了规则中的regexp:
<rule name="rentals by proptype+state+city+street" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9\-+]+)/rent/state/([a-zA-Z\-+]+)/([a-zA-Z0-9%\-+]+)/([a-zA-Z0-9%\-+]+)/([0-9a-zA-Z%\-+']+)$" />
</conditions>
<action type="Rewrite" url="search_new.aspx?proptype={C:1}&state={C:2}&city={C:4}&street={C:5}" />
</rule>
你没有。URL中不允许使用这些内容。您必须先将它们转义,然后才能将它们转换为url。@VDWWD谢谢,您说得对,请参阅我的更新1。你能帮忙吗?也许这会有帮助:@VDWWD谢谢。我确实在使用这个模块。但在阅读了那个页面后,我仍然无法解释为什么我的链接不起作用,并抛出了一个错误。你能解释一下吗?好吧,它对我给出的示例URL有效,但是为什么你的重写规则仍然在这个URL上抛出404:http://www.example.com/bungalow/rent/state/north-dakota/street/savanah/%27s格拉克eland
和这个(不确定querystring参数是否影响您的规则)http://www.example.com/bungalow/rent/state/north-dakota/street/savanah/%27s格拉克eland?pricefrom=2250
对于使用此规则的任何人,请注意{UNENCODED_URL}
也可能包含查询字符串。因此,当URL包含任何查询字符串时,即使它只是一个查询分隔符(?
)。看这里
<rule name="rentals by proptype+state+city+street" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9\-+]+)/rent/state/([a-zA-Z\-+]+)/([a-zA-Z0-9%\-+]+)/([a-zA-Z0-9%\-+]+)/([0-9a-zA-Z%\-+']+)$" />
</conditions>
<action type="Rewrite" url="search_new.aspx?proptype={C:1}&state={C:2}&city={C:4}&street={C:5}" />
</rule>