views:

272

answers:

1

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.

+3  A: 

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.

Darin Dimitrov
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 try
Simon_Weaver
It was a 4.0 Framework project that I created using the Visual Studio template.
Darin Dimitrov