ASP.NET MVC 5自定义路由转向错误操作

时间:2017-05-02 12:18:59

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

我在使用未正确路由的自定义路由时遇到问题。 @Html.ActionLink@Html.RouteLink都会创建正确的网址fantasy/1/fantasyleague1/matchups,点击后它们永远不会触及Matchups操作并错误地路由到fantasy/1/fantasyleague1/settings?round=3

RouteDebugger显示:

  

匹配路线:幻想/ {leagueID} / {leagueSlug} /设置

     

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

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");
    }

1 个答案:

答案 0 :(得分:1)

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

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

301重定向用于确保已经放入野外的URL(即,用户可能已加入书签和/或搜索引擎可能已编入索引)更新到新位置。它们通常不应仅用于在应用程序中将用户从URL A到URL B.