Permanent redirection not working

80 views Asked by At

Having spent a lot of time wondering why it's so hard to do permanent redirection in MVC, I came across this page http://www.eworldui.net/blog/post/2008/04/ASPNET-MVC---Legacy-Url-Routing.aspx which made it look moderately straightforward.

I downloaded the sample code, copied all the helper classes into a new MVC project, and added the following to the RouteConfig.RegisterRoutes() method...

routes.Add("Jim1", new LegacyRoute("Default.aspx", "Home", new LegacyRouteHandler()));

The idea was to have requests for /Default.aspx mapped to /Home instead. However, this just gives a 404 in the browser. When debugging, it breaks on the LegacyHandler.ProcessRequest() method on the line that throws an "Invalid Url" exception.

Has anyone used this code that can advise? Or, does anyone have a SIMPLE solution for this? I have an old ASP.NET WebForms site that I've converted to MVC, and want to set up redirection. I can't believe how difficult it is.

Anyone any ideas?

1

There are 1 answers

0
Avrohom Yisroel On

In case it helps anyone, I did some more searching, and found a fairly simple solution that did work.

If you look at this blog post, you'll find a fairly simple approach that requires you to add some classes to your solution. I created a new class library, and added them there. I then referenced this from my MVC project, and added lines like the following to my RegisterRoutes() method...

routes.MapLegacyRoute(null, "Default.aspx", new {controller = "Home", action = "Index"});
routes.MapLegacyRoute(null, "About.aspx", new {controller = "Home", action = "About"});
routes.MapLegacyRoute(null, "Contact.aspx", new {controller = "Home", action = "Contact"});

Worked like a treat!

Hope this helps someone. It would have saved me hours if I had found this earlier!