这是我的设置示例:
public class UserController : Controller
{
public ActionResult Index(int? id) { ... }
[HttpPost]
public ActionResult DoSomething(int id) { ... }
public ActionResult Search([params]) { ... }
}
我希望能够通过这些路线访问它们:
/app/User/{id}
/app/User/DoSomething/{id}
/app/User/Search/
我尝试像这样设置我的路线,但是如果我尝试导航到/app/User/Search/
或发布到/app/User/DoSomething/
,Index
则改为点击操作。
routes.MapRoute(
name: "UserWithoutIndex",
url: "User/{id}",
defaults: new { controller = "User", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我怎样才能做到这一点?我认为在上面的路由之前指定它自己的路由中的每个特定操作是可行的UserWithoutIndex
,但是我有多个操作,我不想创建一个特定于控制器中每个操作的路由。