1

In an ASP MVC3 project a call to:-

<h2><a href="@Url.RouteUrl("_ScientificPrograms", new { idProgramaSeleccionado = @programa.Id, nombrePrograma = @programa.NombreProgramaUrl(), IdiomaSeleccion = idioma })">@programa.NombrePrograma(@idioma)</a></h2>

corresponding to a registered route in the Global.asax.cs file of:-

routes.MapRoute(
             "_ScientificPrograms", // Route name
             "{IdiomaSeleccion}/research/scientific-programmes/{idProgramaSeleccionado}/{nombrePrograma}/{idGrupoSeleccionado}/{nombreGrupo}", 
             new { controller = "Research", action = "ScientificProgrammes", IdiomaSeleccion = UrlParameter.Optional, idProgramaSeleccionado = UrlParameter.Optional, nombreGrupo = UrlParameter.Optional, idGrupoSeleccionado = UrlParameter.Optional, nombrePrograma = UrlParameter.Optional } 
         );

works fine running on my local machine producing urls like:-

http://localhost/es/research/scientific-programmes/1/molecular-oncology

When the website is deployed, however, the same call only produces

http://deployedsite/es/research/

although entering

http://deployedsite/es/research/scientific-programmes/1/molecular-oncology

does correctly resolve the page.

The same behaviour is observed both on my own test hosting and on my clients QA server. Other routing calls work fine but it appears that this one is some what special.

2 Answers 2

2

You likely have solved this or moved on, but I will put this information here in case someone else comes across your question because they are suffering from the same MVC3 bug that causes this issue.

It turns out that consecutive UrlParameter.Optional parameters causes a small bug in MVC3, as per this blog entry from Phil Haack.

The bug manifests when you have a route with two consecutive optional URL parameters and you attempt to use the route to generate an URL. The incoming request matching behavior is unchanged and continues to work fine.

The workaround is simple. To fix this issue, change the existing route to not have any optional parameters by removing the default values for month and day. This route now handles the first URL where month and day was specified. We then add a new route for the other two cases, but this route only has one optional month parameter.

In my case, I had a route like this:

routes.MapRoute(
            "HouseholdComments",
            "Comments/Household/{id}/{saQid}/{clId}",
            new { controller = "Comments", action = "Household", id = UrlParameter.Optional, saQid = UrlParameter.Optional, clId = UrlParameter.Optional }
        );

And I was trying to use Url.RouteUrl like this:

var questionHref = '@Url.RouteUrl("HouseholdComments", new {id=@Model.SafetyForm.SAS_SEQ_NO})'

thinking that it should just call the route with only 1 parameter. But it did not, it returned null. As in Phil's article above, I had to add a separate route for this case after my HouseholdComments route:

routes.MapRoute(
            "HouseholdCommentsBase",
            "Comments/Household/{id}",
            new {controller = "Comments", action="Household", id=UrlParameter.Optional}
        );

I then was able to use that route in Url.RouteUrl to properly get me the link I needed.

I'm guessing that you should just need to add another route to handle the case where you are only using only those 3 parameters in Url.RouteUrl.

0
0

It turns out that the application of the latest .NET service packs to the server fixed the problem with no code changes. The It-Works-On-My-Machine behaviour turned out to be the smoking gun.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.