繁体   English   中英

ASP.NET MVC 5自定义路由执行错误的操作

[英]ASP.NET MVC 5 custom route goes to wrong action

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

RouteDebugger显示:

匹配的路线:Fantasy / {leagueID} / {leagueSlug} /设置

生成的URL:/ 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");
    }

最有可能的是,这根本不是路由问题。 您正在使用RedirectToActionPermanent ,它会产生301重定向。 大多数浏览器都缓存301重定向,因此您看到的行为很可能是从缓存浏览器的第一个匹配项开始的。

而不是使用的RedirectToActionPermanent ,你应该使用RedirectToAction ,这将产生一个“正常”的302重定向。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM