I have the following url that I need to maintain in my asp.net mvc project for a while.
http:
I need to map this to this URL.
http:
I have already reviewed the following information.
http://blog.eworldui.net/post/2008/04/ASPNET-MVC---Legacy-Url-Routing.aspx
ASP.Net MVC Routing Legacy URLs Passing a Request Request Identifiers for Controller Actions
Trying to follow this, I can get the first url to work, but then all the other urls are broken. Can anyone see mine where I did 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 the code in my global.asax.cs that I use.
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); } } }
c # asp.net-mvc routing
Mhinton
source share