mirror of
https://github.com/rudollee/LearningMVC.git
synced 2025-06-07 16:06:21 +00:00
47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using System.Web.Routing;
|
|
|
|
namespace UrlsAndRoutes.Infrastructure
|
|
{
|
|
public class LegacyRoute : RouteBase
|
|
{
|
|
private string[] urls;
|
|
|
|
public LegacyRoute(params string[] targetUrls)
|
|
{
|
|
urls = targetUrls;
|
|
}
|
|
|
|
public override RouteData GetRouteData(HttpContextBase httpContext)
|
|
{
|
|
RouteData result = null;
|
|
|
|
string requestedUrl = httpContext.Request.AppRelativeCurrentExecutionFilePath;
|
|
if (urls.Contains(requestedUrl, StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
result = new RouteData(this, new MvcRouteHandler());
|
|
result.Values.Add("controller", "Legacy");
|
|
result.Values.Add("action", "GetLegacyURL");
|
|
result.Values.Add("legacyURL", requestedUrl);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
|
|
{
|
|
VirtualPathData result = null;
|
|
|
|
if (values.ContainsKey("legacyURL") && urls.Contains((string)values["legacyURL"], StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
result = new VirtualPathData(this, new UrlHelper(requestContext).Content((string)values["legacyURL"]).Substring(1));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |