简体   繁体   中英

ASP.NET Url Rewriting - require regex for test cases

I need a regular expression for the ASP.NET url rewriting module that will fulfill the following test cases?

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

Can anyone help?

I managed to fudge this with two rules. Not as pretty as I might like but what can you do?

<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>

Closest I came with a single rule was:

^products(?=(?:(?:[a-z0-9\-/]*)/([0-9]+)/$)?)(.[a-z0-9\-/]*)

I'd still be interested in seeing this done in a single rule if anyone's feeling brave...

Here's a try. This does enforce the trailing slash and nonempty attx strings. If you don't need the latter, replace ([^/]+/)* by (.*/)? .

<rule source="products/(([^/]+/)*)([0-9]*)/$"
      destination="products.aspx?Atts=$1&amp;Page=$3"/>
<rule source="products/(([^/]+/)*)$"
      destination="products.aspx?Atts=$1&amp;Page="/>

(This seems to be the style of URL rewriting rules commonly used in ASP.NET; I'm sure you can change them to fit the module you're using).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM