Given a URL rewrite rule like the following:
<rule name="RewriteUserFriendlyThings" stopProcessing="true">
<match url="^cat/sub-cat/(\d+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="cat/sub-cat/detail.aspx?id={R:2}" />
</rule>
In C# code I need to read out the value in the second group of the pattern (which is my ID) for my Bookmark button (don’t ask) to work with pages that are dynamic like this. I’m using a certain CMS which does things at publish time, and we missed out bookmarking dynamic content.
So what I’ve done is load the web.config as XML and match the current URL based on what is in the url attribute of the match element. However, I can’t figure out how to get at the group. Bearing in mind this needs to be generic so in another rule the group could be the third or first group.
I have a white list of rules which I do this against.
I tried using Capture Groups (?<id>\d+) but the web.config doesn’t allow them.
The way I’ve got around this is using the Server Variables feature of the IIS 7 URL Rewriting module.
I defined a new allowed server variable and then captured the value that I needed that way using the IIS URL Rewrite syntax
{R:2}. I could then acccess the value using theHttpContext.Request.ServerVariablescollection.