ASP.NET MVC3所选路由在浏览器中创建了不正确的URL
我有一个ASP.NET MVC3项目(VS2008 Ultimate),其中一个注册的路由行为非常奇怪 首先,从Global.asax.cs方面看它是什么样子ASP.NET MVC3所选路由在浏览器中创建了不正确的URL,asp.net,asp.net-mvc-3,asp.net-mvc-routing,Asp.net,Asp.net Mvc 3,Asp.net Mvc Routing,我有一个ASP.NET MVC3项目(VS2008 Ultimate),其中一个注册的路由行为非常奇怪 首先,从Global.asax.cs方面看它是什么样子 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "H
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);
}
我想我应该看到这样的东西:
http://localhost:51408/VideoPlayer/38/1
除此之外,每条路线都运转良好:
routes.MapRoute(
"VideoPlayer", // Route name
"VideoPlayer/{id}/{page}", // URL with parameters
new { controller = "Video", action = "Player" }
);
我使用了Phil Haack的路由调试器,我可以看到它正在捕获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
);
不知道这里发生了什么。我已经离开这个网站很长一段时间了,但它确实让我非常烦恼,我将感谢任何帮助
如果您希望观看直播:
先谢谢你
马吕斯
这是我的第一个web项目,假设我做错了什么,但不知道是什么。我会使用Url.RouteUrl而不是Url.Action
// VideoPlayer is the RouteName listed in the global.asax
Url.RouteUrl("VideoPlayer", new { id = Model.ID, page = 1 });
谢谢肖恩。Url.RouteUrl创建了一个不错的链接(应该是这样的)另一个问题是Global.asax中的路由映射不正确。它指向动作玩家,应该是“视频玩家”Doh!这些打字错误:-)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// VideoPlayer is the RouteName listed in the global.asax
Url.RouteUrl("VideoPlayer", new { id = Model.ID, page = 1 });