【问题标题】:mvc custom route configurationmvc 自定义路由配置
【发布时间】:2014-01-08 11:24:01
【问题描述】:

假设我们要将旧版本的 asp.net web 表单转换为 mvc 架构。 但有些用户已将我们的网址添加为书签,或者将其链接到其他网站。 现在我们不想放弃那个 urls。相反,我们想创建一个自定义路由来管理旧类型的 url。 这个类从用户那里获取旧的 url 并创建一个新的路由并将用户重定向到新页面。 这是我的自定义路由类:

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)
    {
        return null;
    }
}

这是我的 RegisterRoutes 函数:

 public static void RegisterRoutes(RouteCollection routes)
    {
       // routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.Add(new LegacyRoute("~/articles/windows_3.1_Overview.html", "~/old/.NET_1.0_Class_Library"));
        routes.MapRoute(null, "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

    }

我还创建了“Legacy”控制器和“GetLegacyUrl”动作。 但是当我运行网站并输入

"localhost:14786/articles/windows_3.1_Overview.html" 

在我的浏览器中,我收到 404 Not Found 错误。 问题出在哪里?为什么会出现此错误?

【问题讨论】:

    标签: asp.net-mvc routes asp.net-mvc-routing


    【解决方案1】:

    我通过创建一个查看路由的全局属性解决了这个问题(在Restful Routing),然后如果它意识到路由是“重定向路由”,则执行redirect。在您的情况下,它将是 LegacyRoute。

    public class RedirectFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.IsChildAction)
                return;
    
            var redirect = filterContext.RouteData.Route as RedirectRoute;
            if (redirect != null)
            {
                var helper = new UrlHelper(filterContext.RequestContext);
                var values = new RouteValueDictionary(filterContext.RequestContext.RouteData.Values);
                var merged = new RouteValueDictionary(redirect.DataTokens["new_path"] as RouteValueDictionary);
    
                // keep the values we specified, and add the other routeValues
                // that we we didn't have overrides for.
                foreach (var key in values.Keys.Where(key => !merged.ContainsKey(key)))
                    merged.Add(key, filterContext.RouteData.Values[key]);
    
                var url = helper.RouteUrl(filterContext.RouteData.Values);
                filterContext.Result = new RedirectResult(url, redirect.IsPermanent);
            }
        }
    }
    

    【讨论】:

    • 请告诉我它是否真的解决了您的问题,如果解决了,请将我的答案标记为正确的:)
    • 很遗憾没有。
    【解决方案2】:

    我知道这就像一个月大,但在你的帖子中,你没有提到这个观点。您希望用户到达什么视图?由于GetLegacyUrl,控制器返回的视图是什么?如果有GetLegacyUrl返回的视图,那么你不会得到404。你发布的原始代码是好的。

    【讨论】:

    • 感谢您的回答,我仍在努力,我会尽快编辑我的问题
    猜你喜欢
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多