views:

134

answers:

1

I need a set up a website that accepts URLs from an older web site version and permenantly redirect them to the new site.

I tried this code from Phil Haack's blog but it seems to redirect everything and severely screw up all route generations.

http://haacked.com/archive/2008/12/15/redirect-routes-and-other-fun-with-routing-and-lambdas.aspx

Has anyone got something like this working?

The important paramenters for defining a redirect for me is targetUrl and destinationUrl

e.g.

routes.RedirectPermanently("about/history","about/heritage");

Would permanently redirect visits to /about/history to /about/heritage.

Haacks APi is perfect, but it just doesn't work. The Route object he uses seems to always be accepted for any routeValues.

A: 

You could always use a catchall at the end of your routes which points to a method that does a redirect, like this source:

public class PermanentRedirectResult : ActionResult
{
  private string _url;

  public PermanentRedirectResult(string url)
  {
    _url = url;
  }

  public override void ExecuteResult(ControllerContext context)
  {
      context.HttpContext.Response.StatusCode = 301;
      context.HttpContext.Response.RedirectLocation = _url;
  }
}
Dan Atkinson