开发者

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&amp;state=$2&amp;term=$3&amp;x=$4"/>
<rewrite url="~/(.+)/(.+).aspx" to="~/Default.aspx?city=$1&amp;state=NONE&amp;term=$2&amp;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&amp;state=$2&amp;term=$3&amp;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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜