asp.net-mvcroutespaginationasp.net-mvc-2

Route matching with GetVirtualPath


I've got the following routing in my application...

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  routes.MapRoute(
    "Branding",
    "foo/bar.css",
    new { controller = "DynamicContent", action = "CSS" }
  );

  routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Account", action = "Index", id = UrlParameter.Optional }
  );
}

I'm using Martijn Boland's paging mechanism and it eventually makes the following call:

var virtualPathData = 
  RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary);

I've verified that my controller and action in RequestContext are valid. But once this call is made, virtualPathData references the URL for foo/bar.css. I have verified that URLs map to the correct controller/action. If I comment out the route above leaving the default, virtualPathData's URL corresponds to the right controller/action.

I've also tried the following line and I have the same result.

//var virtualPathData = 
  RouteTable.Routes.GetVirtualPathForArea(this.viewContext.RequestContext, pageLinkValueDictionary);

Can anyone help me figure out what I'm doing wrong?


Solution

  • Unfortunately, I think the logic for generating URLs doesn't handle static URLs very well. I believe one thing you can do is:

    routes.MapRoute(
        "Branding",
        "{controller}/bar.css",
        new { controller = "DynamicContent", action = "CSS" },
        new {controller="foo"}
      );
    

    Try that out.