Explicitcoder

Divyang's ICollection

Legacy Url Routing when application move from webform to MVC

// The legacy route class that exposes a RedirectActionNamepublic class LegacyRoute : Route{
public LegacyRoute(string url, string redirectRuleName):base(url, new LegacyRouteHandler())
{
RedirectRuleName = redirectRuleName;
}

public string RedirectRuleName { get; set; }

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

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

// The legacy HttpHandler that handles the requestpublic class LegacyHandler : IHttpHandler{
public void ProcessRequest(HttpContext context)
{
var requestContext = context.Request.RequestContext;
string redirectActionName = ((LegacyRoute)requestContext.RouteData.Route).RedirectRuleName;

var queryString = requestContext.HttpContext.Request.QueryString;
foreach (var key in queryString.AllKeys)
{
requestContext.RouteData.Values.Add(key, queryString[key]);
}

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

context.Response.Status = "301 Moved Permanently";
context.Response.AppendHeader("Location", data.VirtualPath);
}

public bool IsReusable
{
get { return false; }
}
}
 
 
 And use rout mapping in global.as
 
routes.MapRoute(
"Newroutname", // Route name
"querystringParameter", // URL with parameters
new { controller = "mycontroller", action = "actionname", id = UrlParameter.Optional } // Parameter defaults);
// redirect /oldurl to the Newroutname route.routes.Add("", new LegacyRoute("oldurl", "Newroutname"));

 more details  are at   stefanolson Blog

compile by Divyang Panchasara Sr. Programmer Analyst Hitech OutSourcing

Leave a Reply

Your email address will not be published. Required fields are marked *