Asp.net mvc 如何定义一条没有';不需要在URL中执行操作吗?
定义了一条路线:Asp.net mvc 如何定义一条没有';不需要在URL中执行操作吗?,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,定义了一条路线: routes.MapRoute( name: "SearchRoute", url: "{controller}/{action}/{id}", defaults: new { controller = "Search", action = "Index", id = UrlParameter.Optional } 索引方法: public Act
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
。如何选择?路由有优先权,即:首先定义哪条路由很重要。请尝试以下操作:
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 }
您可以按如下方式创建路线:
routes.MapRoute(
name: "SearchRoute",
url: "{controller}/{id}",
defaults: new { controller = "Search", action = "Index", id = UrlParameter.Optional }
);
它只针对
索引
操作,但听起来像你想要的。你能显示索引方法吗?路由系统无法区分ID和操作名称之间的区别。you SearchViewModel在那里做什么?你的搜索方法最好是HttpPost@SLaks-你可以有一个动作名称“1”?@O.O:是的。(使用[ActionName]
属性)即使不可能,路由引擎也不会知道这一点;就路由引擎而言,action
是可以有任何值的任意参数。这将中断/Search/OtherActionName