URL Rewrite - optional parameter?
I would like a regex开发者_JAVA技巧 string to reflect an optional parameter, in this case, a geographic state.
I can accomplish this with two rules:
<rewrite url="~/(.+)-(.+)/(.+).aspx" to="~/Default.aspx?city=$1&state=$2&term=$3&x=$4"/>
<rewrite url="~/(.+)/(.+).aspx" to="~/Default.aspx?city=$1&state=NONE&term=$2&x=$4"/>
With, a query to /Los+Angeles/cars.aspx: Geo - city: Los Angeles Geo - state: NONE
With, a query to /Los+Angeles-CA/cars.aspx: Geo - city: Los Angeles Geo - state: CA
I'm curious about if there is a way to do this in one rewrite statement.
Thanks.
Have not messed with rewrite url in the config in a while but am quite used to regex. Would this work for you?
<rewrite url="~/([^-]+)(?:-(.+))?/(.+).aspx" to="~/Default.aspx?city=$1&state=$2&term=$3&x=$4"/>
The state would not say none but should actually be blank.
Are you willing to pass a blank string instead of NONE to the state parameter? If so, your 1st rule could be rewritten like this:
~/(.+)-?(.+)/(.+).aspx
...and your 2nd could be deleted.
精彩评论