8

I've looked at ASP.Net MVC routing legacy URLs passing querystring Ids to controller actions and several other similar posts for legacy URL routing, but I can't get past the error "The RouteData must contain an item named 'controller' with a non-empty string value." Looking this up on line didn't give me any hints to solve my problem.

I've implemented the Legacy routing class described in the link above, and this is what I've defined in the routing table:

        routes.Add(
            "Legacy", 
            new LegacyRoute("fooref.aspx", 
            "FooRef", 
            new LegacyRouteHandler())
        );

        routes.MapRoute(
            "FooRef",
            "{controller}/{action}",
            new
            {
                controller = "Home",
                action = "Index",
                foo_id = UrlParameter.Optional,
                bar_id = UrlParameter.Optional
            }
        );

When I use Phil Haack's route debugger, it indicates that fooref.aspx has a match, but when I turn the route debugger off, I get the error above. If I reverse the statement order, I get "Resource not found" for /ctprefer.aspx, which makes sense -- so it appears to be finding that as a valid route when put in the other order.

Where do I need to declare this missing controller reference?

Have routing requirements changed for ASP.NET MVC 2 RTM?

Community
  • 1
  • 1
John Kaster
  • 2,509
  • 1
  • 33
  • 40

2 Answers2

1

The solution to this is to use an IHttpHandler directly, rather than an MVCHandler. I have posted code that works with MVC 3 to my blog: http://www.olsonsoft.com/blogs/stefanolson/post/Handling-Legacy-Urls-in-AspNet-MVC-3.aspx

...Stefan

Stefan
  • 134
  • 1
  • 4
  • 13
  • Thanks, Stefan. This looks interesting. I'm not sure it would work with a data-driven approach to url direction, but it looks like it works for pre-declared lists. I don't know when I'll be able to get back to working on this, but it looks like what you wrote should work, based on code perusal only. I'll mark this as the accepted answer and if I get back to working on this code, I'll give it a full test. – John Kaster Apr 29 '11 at 01:52
0

You might want to take a look at URL Rewrite module for IIS. It can be used to translate legacy URL to your new MVC URLs without 'polluting' your app with legacy routes.

Don't know if it will fit your solution but it's worth having an alternative.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • Jakub, thanks for the suggestion. I am aware of this functionality. I was looking for something controlled at the web application level rather than the server level, since this web application may be hosted on other IIS servers in the future that wouldn't be administered by my team. – John Kaster Jan 05 '11 at 21:03