我看到 MVC 路由存在很多问题,并且在获取匹配 URL 的路由时也遇到了类似的问题。
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute("Beer", "Beer/{beerid}", new { controller = "Beer", action = "Id", beerid = 0});
routes.MapRoute("Beer", "Beer/{beername}", new { controller = "Beer", action = "Name" });
BeerController 方法
public ActionResult Id(int beerid)
public ActionResult Name(string beername)
如果我将方法更改为以下,
public ActionResult Id(int? id)
public ActionResult Name(string id)
默认路由适用于以下 URL:
http://localhost/Beer/Id/100
http://localhost/Beer/Name/Coors
但我想要的只是
http://localhost/Beer/100
http://localhost/Beer/Coors
有任何想法吗?