I have been developing an MVC application locally (IIS Express) and deploing to IIS 7.5 periodically in order to test.
I have just added a new named Route to my Global.asax.cs file:
routes.MapRoute(
"MyCustomRoute", // Route name
"{documentID}/{year}", // URL with parameters
new { controller = "Documents", action = "CurrentVersion", year = DateTime.Now.Year }, // Parameter defaults
new { documentID = @".*\d+.*" } // Regex matches only where documentID contains numerical values.
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Documents", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I have set up a Html.RouteLink using “MyCustomRoute” and all works well on my local machine.
However, when I run this on the Web Server, Html.RouteLink is generating an empty link. In addition, if i enter the URL directly into the browser, it gives a 404.
It seems as if the Route has not registered. What am I missing?
It’s impossible for us to debug your route without seeing the code, and if you use the right tools, it will be very easy for you to see what’s going on and what is being matched.
Phil Haack built an excellent open source tool called RouteDebugger. You can get it through NuGet and read about how to use it at http://haacked.com/archive/2011/04/12/routedebugger-2.aspx . A more robust version of this project is available as RouteMagic and is at Codeplex and git. Details at this blog post
UPDATE
Based on your regex, you’re documentID is not being matched due to greediness.
.*will match everything, so you it will never have the opportunity to match\d+, because the preceding pattern will cannibalize all matches. You can read about regex greediness and laziness at http://www.regular-expressions.info/repeat.html .UPDATE 2
Regexes are probably the thing I’m worst at programming, and the only reason I recognized that issue is because I am so bad at them that I’ve run into the same issue about a million times. That being said, I think
@"(.*?)(\d+?)(.*?)"will do the trick. It should work without any of the parentheses as well (like@".*?\d+?.*?"), but I like to keep them in there for readability (mostly because I’m so bad at them).