定义了一条路线:
routes.MapRoute(
name: "SearchRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Search",
action = "Index", id = UrlParameter.Optional }
索引方法:
public ActionResult Index(int? id, SearchViewModel model)
我宁愿使用上面的2而不必输入/Index/1
。怎么样?
答案 0 :(得分:0)
路由优先,即:您定义的路线首先是重要的。请尝试以下方法:
routes.MapRoute(
name: "SearchRoute",
url: "Search/{id}",
defaults: new { controller = "Search",
action = "Index", id = UrlParameter.Optional }
routes.MapRoute(
name: "DefaultRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
答案 1 :(得分:0)
您可以像这样创建路线:
routes.MapRoute(
name: "SearchRoute",
url: "{controller}/{id}",
defaults: new { controller = "Search", action = "Index", id = UrlParameter.Optional }
);
它只会定位Index
动作,但听起来就像你想要的那样。