根据您提到的文章,这些是完成此操作的步骤:
1-您的 LegacyHandler 必须从查询字符串中提取路线值(在这种情况下,它是艺术家的 id)这是执行此操作的代码:
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)
{
requestContext.RouteData.Values.Add(key, queryString[key]);
}
VirtualPathData path = RouteTable.Routes.GetVirtualPath(requestContext, redirectActionName,
requestContext.RouteData.Values);
httpContext.Response.Status = "301 Moved Permanently";
httpContext.Response.AppendHeader("Location", path.VirtualPath);
}
}
2-您必须将这两个路由添加到 RouteTable 中,您有一个带有 ViewArtist 操作的 ArtistController 接受 int 类型的 id 参数
routes.Add("Legacy", new LegacyRoute("Artists/ViewArtist.aspx", "Artist", new LegacyRouteHandler()));
routes.MapRoute("Artist", "Artist/ViewArtist/{id}", new
{
controller = "Artist",
action = "ViewArtist",
});
现在您可以导航到如下网址:/Artists/ViewArtist.aspx?id=123
您将被重定向到:/Artist/ViewArtist/123