Optional route parameters in ASP.NET 4 RTM no longer work as before
Asked Answered
U

2

6

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", 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 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.

Uphill answered 13/4, 2010 at 3:28 Comment(1)
If anyone could test this in .NET 4 RC I'd really appreciate it. I've only got one machine :-(Uphill
T
5

The correct way of specifying optional parameters in ASP.NET MVC 2.0 is using 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 these 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" })%>

Render as:

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

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

Thumb answered 17/4, 2010 at 19:16 Comment(2)
was this a 3.5 or 4.0 framework project? something changed for sure because I didnt change the mvc version. and i actually thought i'd tested with optional before but actually had it render UrlParameter.Optional to the ouput URL. thanks for testing it out though. i'll give this another tryUphill
It was a 4.0 Framework project that I created using the Visual Studio template.Thumb
C
1

I know the question is for MVC2 but in MVC3, the answer given by Darin Dimitrov will fail.

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

Conga answered 21/6, 2011 at 8:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.