开发者

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

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("gal开发者_StackOverflow社区lery-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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜