mirror of
https://github.com/rudollee/LearningMVC.git
synced 2026-08-11 16:32:55 +00:00
Ch. 16 Routing Adv (until 16-21)
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user