这是我的routeconfig.cs
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("Background/AutoComplete.aspx/{*webmethod}");
routes.IgnoreRoute("Background/Misc.aspx");
routes.MapRoute(
name: "NoAuthorName",
url: "Author",
defaults: new { controller = "Author", action = "Index" });
routes.MapRoute(
name: "AuthorNameonly",
url: "{controller}/{author}",
defaults: new { controller = "Author", action = "AllQuotesByAuthorWithoutAuthorID" });
routes.MapRoute(
name: "AuthorNamewithAuthorID",
url: "{controller}/{author}/{id}",
defaults: new { controller = "Author", action = "AllQuotesByAuthorWithAuthorID"});
routes.MapRoute(
name: "AllQuotesByAuthorWithAuthorIDandQuoteID",
url: "{controller}/{author}/{id}/{id1}",
defaults: new { controller = "Author", action = "AllQuotesByAuthorWithAuthorIDandQuoteID" });
routes.MapRoute(
name: "FinalURLwithAuthor",
url: "{controller}/{author}/{id}/{id1}/{quotesseparatedbyhyphen}",
defaults: new { controller = "Author", action = "Finalurl" });
routes.MapRoute(
name: "NoKeywordName",
url: "Keyword",
defaults: new { controller = "Keyword", action = "Index" });
routes.MapRoute(
name: "AllQuotesByKeywordwithoutKeywordID",
url: "{controller}/{key}",
defaults: new { controller = "Keyword", action = "AllQuotesByKeywordwithoutKeywordID" });
routes.MapRoute(
name: "AllQuotesByKeywordWithKeywordID",
url: "{controller}/{key}/{id}",
defaults: new { controller = "Keyword", action = "AllQuotesByKeywordWithKeywordID" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional });
这是它的工作原理。 http://sitename.com/Author/Benjamin-Quotes/AuthorID - 有效。
http://sitename.com/Author/Benjamin-Quotes/ - 有效。
http://sitename.com/Author - 有效。
但
http://sitename.com/Keyword - 作品
http://sitename.com/Keyword/Love - 不起作用 404. error
http://sitename.com/Keyword/Love/Keywordid - 不起作用。 404. error
然而,当我删除作者的所有地图路线时,只有在routeconfig.cs
中只有这样的关键字 routes.MapRoute(
name: "NoKeywordName",
url: "Keyword",
defaults: new { controller = "Keyword", action = "Index" });
routes.MapRoute(
name: "AllQuotesByKeywordwithoutKeywordID",
url: "{controller}/{key}",
defaults: new { controller = "Keyword", action = "AllQuotesByKeywordwithoutKeywordID" });
routes.MapRoute(
name: "AllQuotesByKeywordWithKeywordID",
url: "{controller}/{key}/{id}",
defaults: new { controller = "Keyword", action = "AllQuotesByKeywordWithKeywordID" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional });
现在http://sitename.com/Keyword - 作品
http://sitename.com/Keyword/Love - 作品
http://sitename.com/Keyword/Love/Keywordid - 有效。
现在,如果我在routeconfig.cs中混合作者路由和关键字路由
以下网址不存在.HTTP 404. error
答案 0 :(得分:3)
AllQuotesByKeywordwithoutKeywordID
和AllQuotesByKeywordWithKeywordID
的路由模式分别与AuthorNameonly
和AuthorNamewithAuthorID
的路由模式相同。您以不同方式命名路由参数的事实不足以区分路由。因此,第一个匹配wins,在这种情况下是AuthorNameonly
和AuthorNamewithAuthorID
。换句话说,当您点击/Keyword/Love
之类的路线时,Keyword
会进入controller
,为您提供正确的控制器,但是,Love
是放入author
param,您对KeywordController
的操作将无法使用,因为它正在寻找key
参数。此外,由于您未指定操作,因此正在使用默认操作,此路由为AllQuotesByAuthorWithoutAuthorID
,这甚至不是KeywordController
上的操作,因此您的404。
长短,你需要别的东西来区分这些路线。您可以将路线更改为:
routes.MapRoute(
name: "AuthorNameonly",
url: "author/{author}",
defaults: new { controller = "Author", action = "AllQuotesByAuthorWithoutAuthorID" });
routes.MapRoute(
name: "AuthorNamewithAuthorID",
url: "author/{author}/{id}",
defaults: new { controller = "Author", action = "AllQuotesByAuthorWithAuthorID"});
和
routes.MapRoute(
name: "AllQuotesByKeywordwithoutKeywordID",
url: "keyword/{key}",
defaults: new { controller = "Keyword", action = "AllQuotesByKeywordwithoutKeywordID" });
routes.MapRoute(
name: "AllQuotesByKeywordWithKeywordID",
url: "keyword/{key}/{id}",
defaults: new { controller = "Keyword", action = "AllQuotesByKeywordWithKeywordID" });
换句话说,不是让controller
参数变量,而是明确指定它。现在,只有URL路径的第一部分匹配时,每条路径才会匹配,从而消除歧义。