Optional route parameters in RTM.NET RTF 4 no longer work

I upgraded my project to ASP.NET 4 RTM using ASP.NET MVC 2.0 RTM today.

I previously used ASP.NET 3.5 with ASP.NET MVC 2.0 RTM .

Some of my routes do not work all of a sudden, and I donโ€™t know why. I'm not sure if something has changed between 3.5 and 4.0 - or is it a regression type problem in RTM 4.0. (I have never tested my application with 4.0 before).

I like to use Url.RouteUrl("route-name", routeParams) to avoid ambiguity when creating URLs. Here is my route definition for the gallery page. I want imageID be optional (you get a thumbnail page if you didn'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", new { galleryID = "cats"}) => /gallery/cats Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4") => /gallery/cats/4 Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles") => /gallery/cats/4/tiddles 

In .NET 4.0 RTM / ASP.NET 2.0 RTM / IIS7

 Url.RouteUrl("gallery-route", new { galleryID = "cats"}) => null Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4") => /gallery/cats/4 Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles") => /gallery/cats/4/tiddles 

Previously, I could only provide galleryID , and everything else would be ignored in the generated URL. But now, it seems, I need to specify all the parameters before the title - or he refuses to determine the URL.

Incoming URLs work fine for /gallery/cats , and it displays correctly in this rule with imageID and title , which are set to null in my controller.

I also tested INCOMING routes from http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx and they all work fine.

+6
asp.net-mvc asp.net-mvc-2 asp.net-routing
source share
2 answers

The correct way to specify optional parameters in ASP.NET MVC 2.0 uses the UrlParameter.Optional field:

 routes.MapRoute( "gallery-route", "gallery/{galleryID}/{imageID}/{title}", new { controller = "Gallery", action = "Index", galleryID = UrlParameter.Optional, imageID = UrlParameter.Optional, title = UrlParameter.Optional } ); 

Assuming the following controller and action:

 public class GalleryController : Controller { public ActionResult Index(string galleryID, string imageID, string title) { return View(); } } 

All of them will work as expected:

 <%= Url.RouteUrl("gallery-route", new { galleryID = "cats" }) %><br/> <%= Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4"}) %><br/> <%= Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles" })%> 

Mark as:

 /gallery/cats /gallery/cats/4 /gallery/cats/4/tiddles 

Note. Tested on Windows 7 x64, Visual Studio 2010 RTM, ASP.NET MVC 2.0 project.

+5
source share

I know the question is for MVC2, but in MVC3, Darin Dimitrovโ€™s answer will fail.

Phil Haack explained the problem and gave a job on his blog: http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

+1
source share

All Articles