【问题标题】:ASP.NET MVC 5 custom route goes to wrong actionASP.NET MVC 5 自定义路由执行错误操作
【发布时间】:2017-09-29 23:13:25
【问题描述】:

我遇到了未正确路由的自定义路由。 @Html.ActionLink@Html.RouteLink 都创建了正确的 url fantasy/1/fantasyleague1/matchups,单击该 URL 时不会触及 Matchups Action 并错误地路由到 fantasy/1/fantasyleague1/settings?round=3

RouteDebugger 显示:

匹配路线:Fantasy/{leagueID}/{leagueSlug}/Settings

使用路由“Fantasy/{leagueID}/{leagueSlug}/Matchups/{round}”生成的网址:/fantasy/11/fantasyleague1/matchups/3

RouteConfig.cs

    routes.MapRoute(
        name: "Fantasy League Matchups",
        url: "Fantasy/{leagueID}/{leagueSlug}/Matchups/{round}",
        defaults: new { controller = "Fantasy", action = "Matchups", leagueSlug = UrlParameter.Optional, round = UrlParameter.Optional },
        constraints: new { leagueID = @"\d+" }
    );

    routes.MapRoute(
        name: "Fantasy League Settings",
        url: "Fantasy/{leagueID}/{leagueSlug}/Settings",
        defaults: new { controller = "Fantasy", action = "Settings", leagueSlug = UrlParameter.Optional },
        constraints: new { leagueID = @"\d+" }
    );

FantasyController.cs

    // GET: /Fantasy/{leagueID}/{leagueSlug}/Matchups/{round}
    public ActionResult Matchups(int leagueID, string leagueSlug = null, int round = -1) {
        var fantasyLeague = DataContext.FantasyLeagues.Where(l => l.ID == leagueID).FirstOrDefault();
        if (fantasyLeague != null) {
            if (string.IsNullOrEmpty(leagueSlug) || round == -1) {
                return RedirectToActionPermanent("Matchups", "Fantasy", new { leagueID = leagueID, leagueSlug = fantasyLeague.Slug, round = fantasyLeague.CurrentRound });
            }

            var userInLeague = User != null && User.Identity != null && fantasyLeague.FantasyTeams.Any(t => t.Owner.UserName == User.Identity.Name);
            var fantasyMatches = fantasyLeague.FantasyMatches.Where(fm => fm.Round == round).ToList();

            return View("Matchups", new FantasyMatchupsViewModel {
                FantasyLeague = fantasyLeague,
                FantasyMatches = fantasyMatches,
                Round = round,
                UserInLeague = userInLeague
            });
        }
        return RedirectToAction("Index");
    }

    // GET: /Fantasy/{leagueID}/{leagueSlug}/Settings
    public ActionResult Settings(int leagueID, string leagueSlug = null) {
        var fantasyLeague = DataContext.FantasyLeagues.Where(l => l.ID == leagueID).FirstOrDefault();
        if (fantasyLeague != null) {
            if (string.IsNullOrEmpty(leagueSlug)) {
                return RedirectToActionPermanent("Settings", "Fantasy", new { leagueID = leagueID, leagueSlug = fantasyLeague.Slug });
            }

            var userOwnsLeague = User != null && User.Identity != null && fantasyLeague.Commissioner.UserName == User.Identity.Name;

            return View("Settings", new FantasySettingsViewModel {
                FantasyLeague = fantasyLeague,
                UserOwnsLeague = userOwnsLeague,
                Name = fantasyLeague.Name,
                MaxPlayers = fantasyLeague.MaxPlayers,
                LockoutPeriod = fantasyLeague.LockoutPeriod,
                PasswordProtected = fantasyLeague.PasswordProtected,
                Password = fantasyLeague.Password
            });
        }
        return RedirectToAction("Index");
    }

【问题讨论】:

  • 我怀疑这根本不是路由问题。您正在使用 RedirectToActionPermanent,它会产生 301 重定向。大多数浏览器缓存 301 重定向,因此您可能看到的行为来自浏览器缓存的第一次命中。很可能您应该将这些更改为RedirectToAction,这会产生一个未缓存的“正常”302 重定向。您将 301 重定向放入控制器似乎很奇怪。
  • @NightOwl888 没错,这是一个缓存的 301 重定向。我只是在遵循我在其他地方读到的内容,即使用 301 添加 slug 会产生更好的 seo,但我并没有完全理解后果。
  • 301 用于 SEO 目的,但它旨在将已经存在的(即已被搜索引擎索引)的 URL 更新为新的(永久) 位置。
  • @NightOwl888 您能否发表此评论作为答案,以便我接受?它引导我清除浏览器缓存以解决路由/重定向问题,并将 RedirectToActionPermanent 转换为 RedirectToAction 以防止它再次抓住我。

标签: c# asp.net-mvc asp.net-mvc-5 url-routing asp.net-mvc-routing


【解决方案1】:

这很可能根本不是路由问题。您正在使用 RedirectToActionPermanent,它会产生 301 重定向。大多数浏览器会缓存 301 重定向,因此您看到的行为很可能是您的浏览器缓存的第一次点击。

您应该使用RedirectToAction,而不是使用RedirectToActionPermanent,这将生成“正常”302 重定向。

301 重定向是为了确保将已被广泛使用的 URL(即用户可能已添加书签和/或搜索引擎可能已编入索引)更新到新位置。它们通常不应仅用于在您的应用程序中将用户从 URL A 获取到 URL B。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2011-04-02
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多