1
我看到很多MVC路由的问题,并且遇到了类似的问题,导致路由匹配URL。MVC 4:自定义路由
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)
默认路由的工作原理与以下网址:
http://localhost/Beer/Id/100
http://localhost/Beer/Name/Coors
但什么我要去的只是
http://localhost/Beer/100
http://localhost/Beer/Coors
任何想法?
我只是去尝试 - 它仍然没有路由..得到一个资源不能被发现:请求的URL:/啤酒/ 110 Id的操作方法是没有被击中.. 我试图创建一个名为BeerById的新操作方法,并使用它来避免与Id方法混淆..它只是没有选择路由规则.. Global.asax.cs断点不会打到 – Jay
@Jay你应该要求* /啤酒/ 110 *不是**啤酒**。您的路线设置完全匹配“啤酒”。 – asymptoticFault
对不起..这是http:// localhost:22449 /啤酒/ 100“啤酒”是一个错字 – Jay