7

I have the following url I need to support in my asp.net mvc project for a while.

http://www.example.com/d.aspx?did=1234

I need to map this to this url.

http://www.example.com/Dispute/Detail/1234

I have already looked at the following information.

http://blog.eworldui.net/post/2008/04/ASPNET-MVC---Legacy-Url-Routing.aspx

ASP.Net MVC routing legacy URLs passing querystring Ids to controller actions

In trying to follow this I can get the first url to work but then all other url's are broken. Can anyone see my where I have gone wrong?

Here are my routes.

    public static void RegisterRoutes(RouteCollection routes)
    {
        // all my other routes

        // Legacy routes
        routes.Add(
          "Legacy", 
          new LegacyRoute(
            "d.aspx", 
            "LegacyDirectDispute", 
            new LegacyRouteHandler())
        );

        routes.MapRoute(
          "LegacyDirectDispute", 
          "Dispute/Details/{id}",
          new { controller = "Dispute", action = "Details", id = "" }
        );

        routes.MapRoute(
          "Default",                                              // Route name
          "{controller}/{action}/{id}",                       // URL with parameters
          new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );
    }

Here is code in my global.asax.cs I am using.

 public class LegacyRoute : Route
 {
  public string RedirectActionName { get; set; }
  public LegacyRoute(string url, string redirectActionName, IRouteHandler routeHandler)
   : base(url, routeHandler)
  {
   RedirectActionName = redirectActionName;
  }
 }

 // The legacy route handler, used for getting the HttpHandler for the request
 public class LegacyRouteHandler : IRouteHandler
 {
  public IHttpHandler GetHttpHandler(RequestContext requestContext) 
  {
   return new LegacyHandler(requestContext);
  }
 }

 // The legacy HttpHandler that handles the request
 public class LegacyHandler : MvcHandler
 {
  private RequestContext requestContext;
  public LegacyHandler(RequestContext requestContext)
   : base(requestContext)
  {
   this.requestContext = requestContext;
  }

  protected override void ProcessRequest(HttpContextBase httpContext)
  {
   string redirectActionName = ((LegacyRoute)RequestContext.RouteData.Route).RedirectActionName;

   var queryString = requestContext.HttpContext.Request.QueryString;
   foreach (var key in queryString.AllKeys)
   {
    if (key.Equals("did"))
    {
     requestContext.RouteData.Values.Add("id", queryString["did"]);
    }
    else
    {
     requestContext.RouteData.Values.Add(key, queryString[key]);
    }
   }

   VirtualPathData path = RouteTable.Routes.GetVirtualPath(requestContext, redirectActionName,
                 requestContext.RouteData.Values);

   if (path != null)
   {
    httpContext.Response.Status = "301 Moved Permanently";
    httpContext.Response.AppendHeader("Location", path.VirtualPath);
   }
  }
 }
3
  • answered here: stackoverflow.com/questions/817325/… Commented Jan 18, 2010 at 21:08
  • @marwan-auida Please read the question content. I have already seen and read that SO question and I even have a link to it in my question. Commented Jan 18, 2010 at 21:13
  • What I mean by my url's being broken is before I add the LegacyRouteHandler code I had links like example.com/News and now they come up example.com/d.aspx?action=Index&controller=News. Commented Jan 18, 2010 at 21:27

3 Answers 3

4

You could just create a file called d.aspx in your site root with contents similar to the following:

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    Response.Redirect(string.Format("http://{0}/Dispute/Detail/{1}", Request.Url.Host, Request.QueryString.Get("did")));
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

@grenade I like this it is the simplest answer I have seen to this problem.
Since this is also for SEO purposes, I recommend the RedirectPermanent method. msdn.microsoft.com/en-us/library/…
2

Here is what I ended up doing to resolve my problem until I saw the simple answer @grenade posted. I found this technique here http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC.

public class LegacyUrlRoute : RouteBase
{
    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        const string status = "301 Moved Permanently";
        var request = httpContext.Request;
        var response = httpContext.Response;
        var legacyUrl = request.Url.ToString();
        var newUrl = "";
        var id = request.QueryString.Count != 0 ? request.QueryString[0] : "";

        if (legacyUrl.Contains("d.aspx"))
        {
            newUrl = "Dispute/Details/" + id;
            response.Status = status;
            response.RedirectLocation = newUrl;
            response.End();
        }
        return null;
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext,
                RouteValueDictionary values)
    {
        return null;
    }
}

public static void RegisterRoutes(RouteCollection routes)
{
    // all my other routes
    routes.Add(new LegacyUrlRoute());
}

Comments

0

How about adding a Map page route:

Routes.MapPageRoute(
            "Legacy", // Route name
            "d.aspx", // URL
            "~/pathfromBaseUrl/d.aspx" // File
            );

Then using this in your aspx/view

Html.RouteLink("MyLinkName", "Legacy")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.