我有一个 ASP.NET MVC3 项目(VS2008 Ultimate),其中一个注册的路由表现得很奇怪。
首先,从 Global.asax.cs 方面看它是怎样的。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home", // Route name
"Home", // URL with parameters
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"BookPage", // Route name
"BookPage/{page}", // URL with parameters
new { controller = "Book", action = "Page" }
);
routes.MapRoute(
"BlogMember", // Route name
"BlogMember/{id}/{page}", // URL with parameters
new { controller = "Blog", action = "Member" }
);
routes.MapRoute(
"BlogPost", // Route name
"BlogPost/{id}/{page}", // URL with parameters
new { controller = "Blog", action = "Post" }
);
routes.MapRoute(
"BlogPage", // Route name
"BlogPage/{page}", // URL with parameters
new { controller = "Blog", action = "Page" }
);
routes.MapRoute(
"BlogTag", // Route name
"BlogTag/{tag}/{page}", // URL with parameters
new { controller = "Blog", action = "Tag" }
);
routes.MapRoute(
"NewBlogPostComment", // Route name
"NewBlogPostComment", // URL with parameters
new { controller = "Blog", action = "NewBlogPostComment" }
);
routes.MapRoute(
"NewBlogPost", // Route name
"NewBlogPost", // URL with parameters
new { controller = "Blog", action = "NewBlogPost" }
);
routes.MapRoute(
"EditBlogPost", // Route name
"EditBlogPost/{id}", // URL with parameters
new { controller = "Blog", action = "EditBlogPost" }
);
routes.MapRoute(
"Account", // Route name
"Account", // URL with parameters
new { controller = "Account", action = "Update" }
);
routes.MapRoute(
"LogOff", // Route name
"LogOff", // URL with parameters
new { controller = "Account", action = "LogOff" }
);
routes.MapRoute(
"LogOn", // Route name
"LogOn", // URL with parameters
new { controller = "Account", action = "LogOn" }
);
routes.MapRoute(
"Register", // Route name
"Register", // URL with parameters
new { controller = "Account", action = "Register" }
);
routes.MapRoute(
"About", // Route name
"About", // URL with parameters
new { controller = "Home", action = "About" }
);
routes.MapRoute(
"UnderConstruction", // Route name
"UnderConstruction", // URL with parameters
new { controller = "Home", action = "UnderConstruction" }
);
routes.MapRoute(
"DisableBlogComment", // Route name
"DisableBlogComment/{id}", // URL with parameters
new { controller = "Blog", action = "DisableBlogComment" }
);
routes.MapRoute(
"DisableAllMemberBlogComments", // Route name
"DisableAllMemberBlogComments/{id}", // URL with parameters
new { controller = "Blog", action = "DisableAllMemberBlogComments" }
);
routes.MapRoute(
"DisableVideoComment", // Route name
"DisableVideoComment/{id}", // URL with parameters
new { controller = "Video", action = "DisableVideoComment" }
);
routes.MapRoute(
"DisableAllMemberVideoComments", // Route name
"DisableAllMemberVideoComments/{id}", // URL with parameters
new { controller = "Video", action = "DisableAllMemberVideoComments" }
);
routes.MapRoute(
"DisableMember", // Route name
"DisableMember/{id}", // URL with parameters
new { controller = "Member", action = "DisableMember" }
);
routes.MapRoute(
"NewPost", // Route name
"NewPost", // URL with parameters
new { controller = "Blog", action = "NewPost" }
);
routes.MapRoute(
"InactiveBlogPosts", // Route name
"InactiveBlogPosts/{page}", // URL with parameters
new { controller = "Blog", action = "InactiveBlogPosts" }
);
routes.MapRoute(
"InactiveBlogComments", // Route name
"InactiveBlogComments/{page}", // URL with parameters
new { controller = "Blog", action = "IncativeBlogComments" }
);
routes.MapRoute(
"EditHomePage", // Route name
"EditHomePage", // URL with parameters
new { controller = "Home", action = "EditHomePage" }
);
routes.MapRoute(
"EditAboutPage", // Route name
"EditAboutPage", // URL with parameters
new { controller = "Home", action = "EditAboutPage" }
);
routes.MapRoute(
"Newsletter", // Route name
"Newsletter", // URL with parameters
new { controller = "Newsletter", action = "Send" }
);
routes.MapRoute(
"Members", // Route name
"Members/{page}", // URL with parameters
new { controller = "Member", action = "MemberList" }
);
routes.MapRoute(
"EditMember", // Route name
"EditMember/{id}", // URL with parameters
new { controller = "Member", action = "EditMember" }
);
routes.MapRoute(
"AppSettings", // Route name
"AppSettings", // URL with parameters
new { controller = "Utility", action = "AppSettings" }
);
routes.MapRoute(
"AudioBookPage", // Route name
"AudioBookPage/{page}", // URL with parameters
new { controller = "Book", action = "AudioBookPage" }
);
routes.MapRoute(
"IPBlocked", // Route name
"IPBlocked", // URL with parameters
new { controller = "Utility", action = "IPBlocked" }
);
routes.MapRoute(
"LiveTV", // Route name
"LiveTV", // URL with parameters
new { controller = "LiveTV", action = "LiveTV" }
);
routes.MapRoute(
"VideoPlayer", // Route name
"VideoPlayer/{id}/{page}", // URL with parameters
new { controller = "Video", action = "Player" }
);
routes.MapRoute(
"Video", // Route name
"Video/{page}", // URL with parameters
new { controller = "Video", action = "VideoList" }
);
routes.MapRoute(
"NewVideoComment", // Route name
"NewVideoComment", // URL with parameters
new { controller = "Video", action = "NewVideoComment" }
);
routes.MapRoute(
"Music", // Route name
"Music", // URL with parameters
new { controller = "Music", action = "Index" }
);
routes.MapRoute(
"FileUpload", // Route name
"FileUpload", // URL with parameters
new { controller = "Utility", action = "FileUpload" }
);
routes.MapRoute(
"PageUnavailable", // Route name
"PageUnavailable", // URL with parameters
new { controller = "Utility", action = "PageUnavailable" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
然后相关控制器:
public ActionResult VideoPlayer(int id, int page)
{
Video post = VideoProvider.GetPost(id);
if (post == null)
return RedirectToAction("PageUnavailable");
// register view, when user not authenticated Name = "";
if (SessionData.settings.LogVideoViews)
{
VideoProvider.RegisterView(id, Request.UserHostAddress);
}
// get comments
int totalRecords;
var vVideoComments = VideoProvider.GetPostComments(id, page, SessionData.settings.ListPositionsPerPage, out totalRecords);
// map to models
var videoPostModel = Mapper.Map<Video, MVC_Web.Models.VideoModels.VideoListModel>(post);
var vVideoCommentsModel = Mapper.Map<List<vVideoComment>, List<MVC_Web.Models.VideoModels.vVideoCommentModel>>(vVideoComments);
ViewBag.postID = id;
ViewBag.route = "VideoPlayer";
ViewBag.EnableComments = SessionData.settings.EnableVideoComments;
ViewBag.selectedMenuItem = "idVideo";
ViewBag.playerWidth = 512;
ViewBag.playerHeight = 384;
ViewBag.comments = vVideoCommentsModel;
ViewBag.page = page;
ViewBag.itemsPerPage = SessionData.settings.ListPositionsPerPage;
ViewBag.totalRecords = totalRecords;
return View(videoPostModel);
}
这就是我创建指向特定页面的链接的方式:
<a href="@Url.Action("VideoPlayer", new { id = Model.ID, page = 1 })">
到目前为止,一切正常,但是当呈现视图时,我在浏览器的地址栏中看到一个 URL,如下所示:
http://localhost:51408/Video/VideoPlayer/38?page=1
我相信我宁愿看到这样的东西:
http://localhost:51408/VideoPlayer/38/1
除了这条路线之外,每条路线都运行良好:
routes.MapRoute(
"VideoPlayer", // Route name
"VideoPlayer/{id}/{page}", // URL with parameters
new { controller = "Video", action = "Player" }
);
我使用了 Phil Haack 的 Route 调试器,我可以看到它正在为 URL 捕获正确的路由:
http://localhost:51408/VideoPlayer/38/1
当尝试不正确的一个时:
http://localhost:51408/Video/VideoPlayer/38?page=1
它使用列表底部的默认路由结束:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
不知道这里发生了什么。我已经离开网站使用这个古怪的 URL 很长时间了,但它确实让我非常困扰,我将不胜感激。
如果您想现场观看:http ://www.voiceofconscience.org/Video/VideoPlayer/39?page=1
提前谢谢马里乌斯
附言。那是我的第一个网络项目,假设我做错了什么,但不知道是什么。