Url routing with two optional parameters unspecified fails on ASP.NET MVC3 RC2 - by simeyla

Status : 

  Duplicate<br /><br />
		This item appears to be a duplicate of another existing Connect or internal item.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 630568 Comments
Status Closed Workarounds
Type Bug Repros 12
Opened 12/11/2010 2:23:26 AM
Access Restriction Public


The following two cases apply to explicitly calling Url.RouteUrl(...) with a named route in ASP.NET MVC 3.0 RC2

      Url.RouteUrl("gallery-route-3", new { galleryID = "cats"});

A URL for a route specifying 3 optional parameters cannot be generated when two of the optional parameters are not specified.

A URL for a route specifying 2 optional parameters cannot be generated when both of the optional parameters are not specified.

Behavior has changed in ASP.NET MVC3 RC2
Sign in to post a comment.
Posted by Damian [MSFT] on 7/6/2012 at 3:43 PM
This has been fixed in ASP.NET 4.5.
Posted by Carl [MSFT] on 4/29/2011 at 10:51 AM
A fix has to be made to the Routing engine, which is not a part of MVC, but the core framework.
Posted by A_t_A on 3/9/2011 at 6:24 AM
It is definately still here :(
Posted by Terje.O on 1/28/2011 at 2:43 AM
It naturally also fails when generating url's using Url.Action(actionName, controllerName);

public class TestController : Controller
    public ActionResult Index()
        var url = Url.Action("Test"); // returns null when it shouldn't.
        return View();
    public ActionResult Test(int id)
     return View();

(Routing is default setup with "id" as UrlParameter.Optional)
Posted by Johannes Hansen on 1/24/2011 at 10:12 AM
I can confirm the validity of this problem. The problem seems to originate in the RouteCollection.GetVirtualPath(RequestContext, string, RouteValueDictionary) method which returns null when it shouldn't. I have created the following steps to repro the problem:

1: Create a new console application (Target Framework: '.NET Framework 4', NOT client profile)
2: Add references to System.Web and System.Web.Routing
3: Paste following code to the program.cs file, overwriting any previous content:

using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

public class Program
    static void Main()
        var httpCtx = new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://localhost/", null), new HttpResponse(new StringWriter())));

        var routes = RouteTable.Routes;
        routes.MapRoute("Test", "root/{test1}/{test2}/{test3}", new { test2 = UrlParameter.Optional, test3 = UrlParameter.Optional });

        var context = new RequestContext(httpCtx , new RouteData());

        var url = new UrlHelper(context);

        var expected1 = "/root/test1";
        var expected2 = "/root/test1/test2";
        var expected3 = "/root/test1/test2/test3";

        var actual1 = url.RouteUrl("Test", new { test1 = "test1" });
        var actual2 = url.RouteUrl("Test", new { test1 = "test1", test2 = "test2" });
        var actual3 = url.RouteUrl("Test", new { test1 = "test1", test2 = "test2", test3 = "test3" });

        var result1 = actual1 == expected1;
        var result2 = actual2 == expected2;
        var result3 = actual3 == expected3;

        Console.WriteLine("Test 1: {0} ({1})", result1 ? "Success" : "Fail", result1 ? string.Format("'{0}'", actual1) : string.Format("Expected '{0}' but was '{1}'", expected1, actual1 ?? "<null>"));
        Console.WriteLine("Test 2: {0} ({1})", result2 ? "Success" : "Fail", result2 ? string.Format("'{0}'", actual2) : string.Format("Expected '{0}' but was '{1}'", expected2, actual2 ?? "<null>"));
        Console.WriteLine("Test 3: {0} ({1})", result3 ? "Success" : "Fail", result3 ? string.Format("'{0}'", actual3) : string.Format("Expected '{0}' but was '{1}'", expected3, actual3 ?? "<null>" ));

4: Run program, and behold the fail
Posted by simeyla on 1/13/2011 at 3:03 PM
This was marked 'Closed - Resolved as Duplicate'

But it is still a problem in MVC3 RTW (final version).

Posted by Microsoft on 12/13/2010 at 11:16 PM
Thanks for your feedback.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by simeyla on 12/13/2010 at 7:27 PM
Here is the complete sample solution zip file

Solution containing 2 projects

1. MVC 3 RC2 (bin deployed)
2. MVC 2 RTM (GAC)

Posted by simeyla on 12/13/2010 at 4:09 PM
I have uploaded the sample project to our webserver which is running :

OS Name    Microsoft® Windows Server® 2008 Standard
Version    6.0.6002 Service Pack 2 Build 6002

http://www.rollingrazor.com/urlparameterbug-mvc3rc2-FAILS/loadeddlls.aspx <-- shows DLLs loaded

** Two of the boxes in the table are blank => incorrect behavior

I also made a copy of the project with ASP.NET MVC 2 to demonstrate that it works as expected :

http://www.rollingrazor.com/urlparameterbug-mvc2rtm-ok/loadeddlls.aspx <-- shows DLLs loaded (must uncheck 'Hide GAC Dlls')

** All boxes are populated => correct behavior

I also confirmed there is NO BUG when PARSING the URL and routing it, only when GENERATING the url with an explicit route name with Url.RouteUrl(.....)
Posted by Microsoft on 12/13/2010 at 1:25 AM
Thank you for reporting this issue. Unfortunately, we are unable to reproduce the issue with the steps you provided.

Could you please provide us with the following information?

1. Some screenshots related this issue. The normal result and the issue result.
2. A sample project zip if possible.

It would be greatly appreciated if you could provide us this information as quickly as possible.

Thank you,
Posted by Microsoft on 12/11/2010 at 2:38 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)