我有一個ASP.NET MVC3項目(VS2008旗艦版),其中一個註冊路由行爲非常奇怪。ASP.NET MVC3選擇的路由在瀏覽器中創建不正確的URL
首先,它是如何從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 })">
截至目前一切工作正常,但是當一個觀點被渲染我見瀏覽器地址欄中的網址如下所示:
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" }
);
我用菲爾哈克的路線調試器,我可以看到它正趕上正確的路線網址:
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
);
不知道這裏發生了什麼。我已經離開了這個古怪的網址工作很長一段時間,但它確實讓我感到非常困擾,我很感激任何幫助。
如果你想現場觀看:http://www.voiceofconscience.org/Video/VideoPlayer/39?page=1
預先感謝您 的Mariusz
PS。這是我第一個網絡項目,並假設我做錯了什麼,但不知道是什麼。
你需要一個404處理器=( – Tyrsius 2012-07-20 20:20:54