【发布时间】: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