Optional route parameters in ASP.NET 4 RTM no longer work as before
Posted
by Simon_Weaver
on Stack Overflow
See other posts from Stack Overflow
or by Simon_Weaver
Published on 2010-04-13T03:28:54Z
Indexed on
2010/04/13
3:32 UTC
Read the original article
Hit count: 432
I upgraded my project to ASP.NET 4 RTM
with ASP.NET MVC 2.0 RTM
today.
I was previously using ASP.NET 3.5
with ASP.NET MVC 2.0 RTM
.
Some of my routes don't work suddenly and I don't know why. I'm not sure if something changed between 3.5 and 4.0 - or if this was a regression type issue in the 4.0 RTM. (I never previously tested my app with 4.0).
I like to use Url.RouteUrl("route-name", routeParams)
to avoid ambiguity when generating URLs. Here's my route definition for a gallery page. I want imageID
to be optional (you get a thumbnail page if you don't specify it).
// gallery id
routes.MapRoute(
"gallery-route",
"gallery/{galleryID}/{imageID}/{title}",
new { controller = "Gallery", action = "Index",
galleryID = (string) null,
imageID = (string) null,
title = (string) null}
);
In .NET 3.5 / ASP.NET 2.0 RTM / IIS7
Url.RouteUrl("gallery-route", "cats") => /gallery/cats
Url.RouteUrl("gallery-route", "cats", 4) => /gallery/cats/4
Url.RouteUrl("gallery-route", "cats", 4, "tiddles") => /gallery/cats/4/tiddles
In .NET 4.0 RTM / ASP.NET 2.0 RTM / IIS7
Url.RouteUrl("gallery-route", "cats") => null
Url.RouteUrl("gallery-route", "cats", 4) => /gallery/cats/4
Url.RouteUrl("gallery-route", "cats", 4, "tiddles") => /gallery/cats/4/tiddles
Previously I could supply only the galleryID
and everything else would be ignored in the generated URL. But now it's looking like I need to specify all the parameters up until title
- or it gives up in determining the URL.
Incoming URLs work fine for /gallery/cats
and that is correctly mapped through this rule with imageID
and title
both being assigned null
in my controller.
I also tested the INCOMING routes with http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx and they all work fine.
© Stack Overflow or respective owner