AttributeRouting icon indicating copy to clipboard operation
AttributeRouting copied to clipboard

Default values

Open delight-by opened this issue 12 years ago • 4 comments
trafficstars

I've just upgraded from v2.5.3 and my whole application's routing just stopped working. Besides issue 232 (declaring default now doesn't cause parameter to become optional), it appeared that defaults don't work at all now - all of the skipped optional parameters get null value. See https://dl.dropbox.com/u/20058165/AttributeRoutingDefaults.rar - very simple MVC app that shows the bug.

delight-by avatar Mar 28 '13 19:03 delight-by

Checking it out. Did you read this: https://github.com/mccalltd/AttributeRouting/wiki/Upgrading-to-v3

mccalltd avatar Mar 28 '13 19:03 mccalltd

You may have been bitten by issue #43.

That problem was due to this bug: http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx.

The workaround, if this applies to you, is to start using more specific routes, so you don't get the third section timebomb:

[Route("export/{type=details}/{period=week-this}/{filters?}")]
[Route("export/{type=details}/{period=week-this}")]
// Your action

I will be updating the wiki and website with this warning.

Also, just a tip: a default is an optional, so you don't need {param=value?} or {param=?}. AR should complain when you do this, but it doesn't. All you need to do is {param=value} or {param?}.

Let me know if this applies.

mccalltd avatar Mar 28 '13 20:03 mccalltd

Well, first of all, as I said, it was working perfectly 7 months ago with MVC 4 and AR v2.5.3, I've only swapped AR binaries today.

I investigated a bit more and found out that "{filters=}" segment in the end causes this behavior. So if only "report/{type=details}/{period=week-this}" is left route works as expected - default value actually makes parameter optional and action is called with proper parameters. So only the processing of parameters with String.Empty as default value is different (and yes, actually one can replace it with {filters?} with minor code change and everything will work again).

As for my original concern, it turns out that assigning parameter a default value AND an optional mark makes parameter forget the default value. This might be the bug too.

delight-by avatar Mar 28 '13 21:03 delight-by

I think it is a bug. Somehow the precedence of defaults and optionals must have changed from v2.x to 3.x or thereabouts. Thanks for reporting it!

mccalltd avatar Mar 28 '13 21:03 mccalltd