C# ASP.NET Url重写-测试用例需要正则表达式

C# ASP.NET Url重写-测试用例需要正则表达式,c#,asp.net,regex,url-rewriting,C#,Asp.net,Regex,Url Rewriting,我需要一个用于ASP.NET url重写模块的正则表达式,它将满足以下测试用例 products/ products.aspx?Atts=&Page= products/att1/ products.aspx?Atts=att1/&Page= products/att1/att2/ products.aspx?Atts=att1/att2/&Page= products/2/

我需要一个用于ASP.NET url重写模块的正则表达式,它将满足以下测试用例

products/                  products.aspx?Atts=&Page=
products/att1/             products.aspx?Atts=att1/&Page=
products/att1/att2/        products.aspx?Atts=att1/att2/&Page=
products/2/                products.aspx?Atts=&Page=2
products/att1/2/           products.aspx?Atts=att1/&Page=2
products/att1/att2/2/      products.aspx?Atts=att1/att2/&Page=2

有人能帮忙吗?

试试看。这会强制执行尾部斜杠和非空attx字符串。如果不需要后者,请将
([^/]+/)*
替换为
(.*/)?



(这似乎是ASP.NET中常用的URL重写规则的风格;我相信您可以更改它们以适应您使用的模块)。

我设法用两条规则来回避这一点。没有我喜欢的那么漂亮,但你能做什么

<rule name="ProductsPagingRule" stopProcessing="false">
  <match url="^products([a-z0-9\-/]*)(?:/([0-9]+)/)"/>
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="products{R:1}/?Page={R:2}" />
</rule>

<rule name="ProductsRule" stopProcessing="true">
  <match url="^products/([a-z0-9\-/]*)/" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="products.aspx?PageId={R:1}" />
</rule>

如果有人觉得自己很勇敢,我仍然有兴趣在一条规则中看到这一点。

有几件事我忘了提。。。。1) 它必须强制执行尾部斜杠。2) url结构中可以有任意数量的“attx/”文件夹。既然这似乎是您要找的,您愿意付多少钱让我帮您做这件事?您的regexp引擎中是否有非贪婪匹配项,并且您能否使用未定义的捕获而不出错?如果是这样,
^products/(?:[a-z0-9-]+/)*?(?:([0-9]*)/)?$
可能适合您。此外,您的解决方案不符合您自己的规范。。。
<rule name="ProductsPagingRule" stopProcessing="false">
  <match url="^products([a-z0-9\-/]*)(?:/([0-9]+)/)"/>
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="products{R:1}/?Page={R:2}" />
</rule>

<rule name="ProductsRule" stopProcessing="true">
  <match url="^products/([a-z0-9\-/]*)/" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="products.aspx?PageId={R:1}" />
</rule>
^products(?=(?:(?:[a-z0-9\-/]*)/([0-9]+)/$)?)(.[a-z0-9\-/]*)