URL: http://www.nickkusters.com/SpeedTouch.aspx
protected void Application_Start()
{
// Legacy Reroutes
Dictionary<String, String> ReRoutes = new Dictionary<string,string>();
ReRoutes.Add("/speedtouch.aspx", "/Services/SpeedTouch");
ReRoutes.Add("/Articles/79/online_speedtouch_wpa_key_lookup", "/Services/SpeedTouch");
Dictionary<String, String> RegexReplace = new Dictionary<string,string>();
RegexReplace.Add("^/css/(.*)", "/Content/$1");
RegexReplace.Add("^/images/(.*)", "/Content/images/$1");
RegexReplace.Add("^/articles/(\\d+)/(.*)\\.aspx", "/Articles/$1/$2");
RegisterRoutes(RouteTable.Routes);
CustomRouter.ReAssignHandler(RouteTable.Routes, ReRoutes, RegexReplace);
}
Пользовательский маршрутизатор:
public class CustomRouter : System.Web.Mvc.MvcHandler
{
internal static Dictionary<String, String> Custom301s;
internal static Dictionary<String, String> Custom301Regexes;
public CustomRouter(System.Web.Routing.RequestContext Context) : base(Context)
{
}
public static void ReAssignHandler(System.Web.Routing.RouteCollection routes, Dictionary<String, String> CustomRoutes, Dictionary<String, String> CustomRouteRegexes)
{
using(routes.GetReadLock())
{
Custom301s = CustomRoutes;
Custom301Regexes = CustomRouteRegexes;
foreach (System.Web.Routing.Route Route in routes)
{
if(Route != null && Route.RouteHandler.GetType() != typeof(CustomRouteHandler))
{
Route.RouteHandler = new CustomRouteHandler();
}
}
}
}
protected override void ProcessRequest(System.Web.HttpContextBase httpContext)
{
String OriginalUrl, Url, UrlPath;
OriginalUrl = httpContext.Request.Url.AbsoluteUri;
Url = OriginalUrl;
UrlPath = httpContext.Request.Url.AbsolutePath.ToLower();
if(UrlPath.StartsWith("/WebResource.axd") )
{
return;
}
if (Url.ToLower().StartsWith("http://buyinbulk.nl"))
{
Url = Url.ToLower().Replace("http://", "http://www.");
}
else if (Custom301s.ContainsKey(Url) )
{
Url = Custom301s[Url];
}
else if( Custom301s.ContainsKey(UrlPath) )
{
Url = Url.ToLower().Replace(UrlPath, Custom301s[UrlPath]);
}
else
{
foreach (String Pattern in Custom301Regexes.Keys)
{
if (Regex.Match(UrlPath, Pattern, RegexOptions.IgnoreCase).Success )
{
Url = Url.ToLower().Replace(UrlPath, Regex.Replace(UrlPath, Pattern, Custom301Regexes[Pattern], RegexOptions.IgnoreCase));
break;
}
}
}
if(Url != OriginalUrl)
{
//mark as 301
httpContext.Response.Status = "301 Moved Permanently";
httpContext.Response.StatusCode = 301;
httpContext.Response.AppendHeader("Location", Url);
}
else
{
try
{
base.ProcessRequest(httpContext);
}
catch (Exception Ex)
{
System.Diagnostics.Debug.WriteLine(String.Format("Error Processing '{0}':\r\n{1}", OriginalUrl, Ex.ToString()));
throw;
}
}
}
}
Теперь мой журнал показывает:
Url: http://www.nickkusters.com/SpeedTouch.aspx
Message: System.Web.HttpException (0x80004005): The controller for path '/SpeedTouch.aspx' was not found or does not implement IController.
Stack Trace:
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<BeginProcessRequest>b__2()
at System.Web.Mvc.SecurityUtil.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a()
at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust[TResult](Func`1 func)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Но если я открою URL-адрес из разных мест, он будет отлично работать ...